COMM_VR.cpp
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <pthread.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <json-c/json.h>
#include "COMM.h"
static recv_callback_t _s_recv_callback = NULL;
static int _s_fd = 0;
static int _s_clientFd = 0;
static pthread_mutex_t _s_send_lock = PTHREAD_MUTEX_INITIALIZER;
static void *thread_recv_from_navi(void * arg)
{
struct sockaddr_un _clientAddr;
socklen_t _len;
char _data[128] = {0};
int _dataLen = 0;
while((_s_clientFd = accept( _s_fd, (struct sockaddr*)&_clientAddr, &_len)) != -1){
while((_dataLen = recv( _s_clientFd, _data, 23, 0)) > 0 ){
if(!strncmp(_data,"/tmp/Navi2VR_Req",16) && _s_recv_callback){
_s_recv_callback(NAVI2VR_REQUEST_T, _data);
}else if(!strncmp(_data,"/tmp/Navi2VR_Ntf",16) && _s_recv_callback){
_s_recv_callback(NAVI2VR_NOTIFY_T, _data);
}else if(!strncmp(_data,"/tmp/Navi2VR_Rsp",16) && _s_recv_callback){
_s_recv_callback(NAVI2VR_NOTIFY_T, _data);
}
memset( _data, 0, sizeof(_data));
}
printf("client disconnect11.\n");
close( _s_clientFd );
_s_clientFd = 0;
unlink( _clientAddr.sun_path);
}
return NULL;
}
int VR_create_server(recv_callback_t callback)
{
_s_recv_callback = callback;
pthread_mutex_init(&_s_send_lock, NULL);
struct sockaddr_un _addr;
_s_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if(_s_fd <= 0){
printf("socket error.\n");
return -1;
}
_addr.sun_family = AF_UNIX;
strcpy(_addr.sun_path, SOCKET_ADDRESS);
if(0 != bind(_s_fd, (struct sockaddr *)&_addr, sizeof(_addr))){
printf("bind error.\n");
return -2;
}
if(-1 == listen(_s_fd, 1)){
printf("listen error.\n");
return -3;
}
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&tid, &attr, thread_recv_from_navi, NULL);
return 0;
}
int VR_send_to_navi(int type, const char * json)
{
static int a = 0, b = 0, c = 0;
char file[1024] = {0};
int len = 0;
if(_s_clientFd > 0){
struct json_object * j_json = json_tokener_parse((const char *)json);
if(VR2NAVI_REQUEST_T == type){
sprintf(file, "/tmp/VR2Navi_Req_%d.json", a);
a = (a + 1)%10;
}else if(VR2NAVI_RESPONSE_T == type){
sprintf(file, "/tmp/VR2Navi_Rsp_%d.json", b);
b = (b + 1)%10;}
else if(VR2NAVI_NOTIFY_T == type){
sprintf(file, "/tmp/VR2Navi_Ntf_%d.json", c);
c = (c + 1)%10;
}
json_object_to_file(file,j_json);
pthread_mutex_lock(&_s_send_lock);
len = send( _s_clientFd, file, strlen(file), 0 );
pthread_mutex_unlock(&_s_send_lock);
return len;
}else{
printf("client disconnect.\n");
return -1;
}
}
COMM_Navi.cpp
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <pthread.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <json-c/json.h>
#include "COMM.h"
static recv_callback_t _s_recv_callback = NULL;
static int _s_fd = 0;
static pthread_mutex_t _s_send_lock = PTHREAD_MUTEX_INITIALIZER;
static void *thread_recv_from_VR(void * arg)
{
char _data[128] = {0};
int _dataLen = 0;
while((_dataLen = recv( _s_fd, _data, 23, 0)) > 0 ){
if(!strncmp(_data,"/tmp/VR2Navi_Req",16) && _s_recv_callback){
_s_recv_callback(VR2NAVI_REQUEST_T, _data);
}else if(!strncmp(_data,"/tmp/VR2Navi_Ntf",16) && _s_recv_callback){
_s_recv_callback(VR2NAVI_NOTIFY_T, _data);
}else if(!strncmp(_data,"/tmp/VR2Navi_Rsp",16) && _s_recv_callback){
_s_recv_callback(VR2NAVI_NOTIFY_T, _data);
}
memset( _data, 0, sizeof(_data));
}
close( _s_fd);
_s_fd = 0;
return NULL;
}
int Navi_connect_VR(recv_callback_t callback)
{
_s_recv_callback = callback;
pthread_mutex_init(&_s_send_lock, NULL);
struct sockaddr_un _addr;
_s_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if(_s_fd <= 0){
printf("socket error.\n");
return -1;
}
_addr.sun_family = AF_UNIX;
strcpy(_addr.sun_path, SOCKET_ADDRESS);
if(connect(_s_fd, (struct sockaddr *)&_addr, sizeof(_addr)) < 0){
printf("connect server error.\n");
close( _s_fd);
_s_fd = 0;
return -1;
}
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&tid, &attr, thread_recv_from_VR, NULL);
return 0;
}
int Navi_send_to_VR(int type,const char * json)
{
static int a = 0, b = 0, c = 0;
char file[1024] = {0};
int len = 0;
if(_s_fd > 0){
struct json_object * j_json = json_tokener_parse((const char *)json);
if(NAVI2VR_REQUEST_T == type){
sprintf(file, "/tmp/Navi2VR_Req_%d.json", a);
a = (a + 1)%10;
}else if(NAVI2VR_RESPONSE_T == type){
sprintf(file, "/tmp/Navi2VR_Rsp_%d.json", b);
b = (b + 1)%10;}
else if(NAVI2VR_NOTIFY_T == type){
sprintf(file, "/tmp/Navi2VR_Ntf_%d.json", c);
c = (c + 1)%10;
}
json_object_to_file(file,j_json);
pthread_mutex_lock(&_s_send_lock);
len = send( _s_fd, file, strlen(file), 0 );
pthread_mutex_unlock(&_s_send_lock);
return len;
}else{
printf("server disconnect.\n");
return -1;
}
}
Navi_test.cpp
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <json-c/json.h>
#include "COMM.h"
void recv_from_VR_callback(int type, char *filename)
{
printf("filename = %s\n", filename);
struct json_object * j_info = json_object_from_file(filename);
struct json_object * _value = NULL;
int cmd = -1;
if(json_object_object_get_ex(j_info, "cmd", &_value))
{
cmd = json_object_get_int(_value);
}
const char *json = NULL;
printf("cmd = %d\n", cmd);
switch (cmd){
case 1000:
json = "{\"cmd\":1000,\"result\":0,\"list\":[{\"pid\":\"MAPIHPJPQAHRJNZWTRTNC\",\"name\":\"湖锦酒楼锦江店\",\"location\":\"114.30593,30.56216\",\"nlonlat\":\"114.30586,30.562191\",\"address\":\"湖北省武汉市武昌区临江大道88附17\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"武昌区\",\"phone\":\"027-88221777|027-88229977\",\"type\":\"中餐馆\",\"typecode\":\"110,100\",\"distance\":\"826.0051\",\"brand\":\"湖锦酒楼\",\"tag\":\"湘菜,鄂菜\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}},{\"pid\":\"MAPICBCRTIHAZNCPJIXIC\",\"name\":\"威斯汀行政俱乐部\",\"location\":\"114.30779,30.5647\",\"nlonlat\":\"114.30767,30.56476\",\"address\":\"湖北省武汉市武昌区临江大道96号武汉富力威斯汀酒店21层\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"武昌区\",\"phone\":\"027-88168888\",\"type\":\"酒吧\",\"typecode\":\"140,100\",\"distance\":\"889.0102\",\"brand\":\"\",\"tag\":\"\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}},{\"pid\":\"MAPICBQOHELVSNFWXIC\",\"name\":\"辉辉鱼庄\",\"location\":\"114.29415,30.56871\",\"nlonlat\":\"114.29401,30.5688\",\"address\":\"湖北省武汉市江汉区沿江大道江滩7\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"江汉区\",\"phone\":\"15927130488\",\"type\":\"中餐馆\",\"typecode\":\"110,100\",\"distance\":\"518.0305\",\"brand\":\"\",\"tag\":\"\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}},{\"pid\":\"MAPIHPZNYWTESPQMHATNC\",\"name\":\"七杯咖啡\",\"location\":\"114.29321,30.57016\",\"nlonlat\":\"114.29326,30.57027\",\"address\":\"湖北省武汉市江汉区民权路39\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"江汉区\",\"phone\":\"15717168204\",\"type\":\"咖啡馆\",\"typecode\":\"160,100\",\"distance\":\"687.5482\",\"brand\":\"\",\"tag\":\"\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}}]}";
break;
case 1001:
json = "{\"cmd\":1001,\"result\":0,\"route_cnt\":3}";
break;
case 1002:
json = "{\"cmd\":1002,\"result\":0}";
break;
case 1003:
json = "{\"cmd\":1003,\"status\":1}";
break;
case 1004:
json = "{\"cmd\":1004,\"direction\":90}";
break;
case 1005:
json = "{\"cmd\":1005,\"route\":\"116.30537,40.05356;116.354351,39.97625;116.351601,40.04688\"}";
break;
case 1006:
json = "{\"cmd\":1006,\"result\":0}";
break;
case 1007:
json = "{\"cmd\":1007,\"result\":0}";
break;
case 1009:
json = "{\"cmd\":1009,\"result\":0,\"status\":2,\"list\":[{\"pid\":\"MAPIHPJPQAHRJNZWTRTNC\",\"index\":0,\"name\":\"湖锦酒楼锦江店\",\"location\":\"114.30593,30.56216\",\"nlonlat\":\"114.30586,30.562191\",\"address\":\"湖北省武汉市武昌区临江大道88附17\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"武昌区\",\"phone\":\"027-88221777|027-88229977\",\"type\":\"中餐馆\",\"typecode\":\"110,100\",\"distance\":\"826.0051\",\"brand\":\"湖锦酒楼\",\"tag\":\"湘菜,鄂菜\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}},{\"pid\":\"MAPICBCRTIHAZNCPJIXIC\",\"index\":1,\"name\":\"威斯汀行政俱乐部\",\"location\":\"114.30779,30.5647\",\"nlonlat\":\"114.30767,30.56476\",\"address\":\"湖北省武汉市武昌区临江大道96号武汉富力威斯汀酒店21层\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"武昌区\",\"phone\":\"027-88168888\",\"type\":\"酒吧\",\"typecode\":\"140,100\",\"distance\":\"889.0102\",\"brand\":\"\",\"tag\":\"\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}},{\"pid\":\"MAPICBQOHELVSNFWXIC\",\"index\":2,\"name\":\"辉辉鱼庄\",\"location\":\"114.29415,30.56871\",\"nlonlat\":\"114.29401,30.5688\",\"address\":\"湖北省武汉市江汉区沿江大道江滩7\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"江汉区\",\"phone\":\"15927130488\",\"type\":\"中餐馆\",\"typecode\":\"110,100\",\"distance\":\"518.0305\",\"brand\":\"\",\"tag\":\"\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}},{\"pid\":\"MAPIHPZNYWTESPQMHATNC\",\"index\":3,\"name\":\"七杯咖啡\",\"location\":\"114.29321,30.57016\",\"nlonlat\":\"114.29326,30.57027\",\"address\":\"湖北省武汉市江汉区民权路39\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"江汉区\",\"phone\":\"15717168204\",\"type\":\"咖啡馆\",\"typecode\":\"160,100\",\"distance\":\"687.5482\",\"brand\":\"\",\"tag\":\"\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}}]}";
break;
case 1010:
json = "{\"cmd\":1010,\"result\":0,\"route_cnt\":3}";
break;
case 1012:
json = "{\"cmd\":1012,\"result\":0}";
break;
case 1013:{
int type = -1;
if(json_object_object_get_ex(j_info, "type", &_value))
{
type = json_object_get_int(_value);
}
if(type == 1)
json = "{\"cmd\":1013,\"type\":1,\"if_set\":0,\"result\":0,\"address\":{}}";
else
json = "{\"cmd\":1013,\"type\":2,\"if_set\":0,\"result\":0,\"address\":{}}";
break;
}
case 1014:
json = "{\"cmd\":1014,\"result\":0,\"route_cnt\":3}";
break;
case 1015:
json = "{\"cmd\":1015,\"result\":0}";
break;
case 1016:
json = "{\"cmd\":1016,\"result\":0}";
break;
case 1017:{
int type = -1;
if(json_object_object_get_ex(j_info, "type", &_value))
{
type = json_object_get_int(_value);
}
if(type == 1)
json = "{\"cmd\":1017,\"type\":1,\"result\":0,\"list\":[{\"pid\":\"MAPIHPJPQAHRJNZWTRTNC\",\"name\":\"湖锦酒楼锦江店\",\"location\":\"114.30593,30.56216\",\"nlonlat\":\"114.30586,30.562191\",\"address\":\"湖北省武汉市武昌区临江大道88附17\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"武昌区\",\"phone\":\"027-88221777|027-88229977\",\"type\":\"中餐馆\",\"typecode\":\"110,100\",\"distance\":\"826.0051\",\"brand\":\"湖锦酒楼\",\"tag\":\"湘菜,鄂菜\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}},{\"pid\":\"MAPICBCRTIHAZNCPJIXIC\",\"name\":\"威斯汀行政俱乐部\",\"location\":\"114.30779,30.5647\",\"nlonlat\":\"114.30767,30.56476\",\"address\":\"湖北省武汉市武昌区临江大道96号武汉富力威斯汀酒店21层\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"武昌区\",\"phone\":\"027-88168888\",\"type\":\"酒吧\",\"typecode\":\"140,100\",\"distance\":\"889.0102\",\"brand\":\"\",\"tag\":\"\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}},{\"pid\":\"MAPICBQOHELVSNFWXIC\",\"name\":\"辉辉鱼庄\",\"location\":\"114.29415,30.56871\",\"nlonlat\":\"114.29401,30.5688\",\"address\":\"湖北省武汉市江汉区沿江大道江滩7\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"江汉区\",\"phone\":\"15927130488\",\"type\":\"中餐馆\",\"typecode\":\"110,100\",\"distance\":\"518.0305\",\"brand\":\"\",\"tag\":\"\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}},{\"pid\":\"MAPIHPZNYWTESPQMHATNC\",\"name\":\"七杯咖啡\",\"location\":\"114.29321,30.57016\",\"nlonlat\":\"114.29326,30.57027\",\"address\":\"湖北省武汉市江汉区民权路39\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"江汉区\",\"phone\":\"15717168204\",\"type\":\"咖啡馆\",\"typecode\":\"160,100\",\"distance\":\"687.5482\",\"brand\":\"\",\"tag\":\"\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}}]}";
else
json = "{\"cmd\":1017,\"type\":2,\"result\":0,\"list\":[{\"pid\":\"MAPIHPJPQAHRJNZWTRTNC\",\"name\":\"湖锦酒楼锦江店\",\"location\":\"114.30593,30.56216\",\"nlonlat\":\"114.30586,30.562191\",\"address\":\"湖北省武汉市武昌区临江大道88附17\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"武昌区\",\"phone\":\"027-88221777|027-88229977\",\"type\":\"中餐馆\",\"typecode\":\"110,100\",\"distance\":\"826.0051\",\"brand\":\"湖锦酒楼\",\"tag\":\"湘菜,鄂菜\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}},{\"pid\":\"MAPICBCRTIHAZNCPJIXIC\",\"name\":\"威斯汀行政俱乐部\",\"location\":\"114.30779,30.5647\",\"nlonlat\":\"114.30767,30.56476\",\"address\":\"湖北省武汉市武昌区临江大道96号武汉富力威斯汀酒店21层\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"武昌区\",\"phone\":\"027-88168888\",\"type\":\"酒吧\",\"typecode\":\"140,100\",\"distance\":\"889.0102\",\"brand\":\"\",\"tag\":\"\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}},{\"pid\":\"MAPICBQOHELVSNFWXIC\",\"name\":\"辉辉鱼庄\",\"location\":\"114.29415,30.56871\",\"nlonlat\":\"114.29401,30.5688\",\"address\":\"湖北省武汉市江汉区沿江大道江滩7\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"江汉区\",\"phone\":\"15927130488\",\"type\":\"中餐馆\",\"typecode\":\"110,100\",\"distance\":\"518.0305\",\"brand\":\"\",\"tag\":\"\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}},{\"pid\":\"MAPIHPZNYWTESPQMHATNC\",\"name\":\"七杯咖啡\",\"location\":\"114.29321,30.57016\",\"nlonlat\":\"114.29326,30.57027\",\"address\":\"湖北省武汉市江汉区民权路39\",\"pname\":\"湖北省\",\"cityname\":\"武汉市\",\"adname\":\"江汉区\",\"phone\":\"15717168204\",\"type\":\"咖啡馆\",\"typecode\":\"160,100\",\"distance\":\"687.5482\",\"brand\":\"\",\"tag\":\"\",\"rinfo\":\"\",\"photo\":[],\"extend\":{}}]}";
break;
}
case 1020:
json = "{\"cmd\":1020,\"result\":0,\"distance\":5.5,\"time\":15}";
break;
case 1021:
json = "{\"cmd\":1021,\"result\":0}";
break;
case 1022:{
int operation = -1;
if(json_object_object_get_ex(j_info, "operation", &_value))
{
operation = json_object_get_int(_value);
}
if(operation == 1)
json = "{\"cmd\":1022,\"operation\":1,\"result\":0}";
else
json = "{\"cmd\":1022,\"operation\":2,\"result\":0}";
break;
}
case 1023:{
int operation = -1;
if(json_object_object_get_ex(j_info, "operation", &_value))
{
operation = json_object_get_int(_value);
}
if(operation == 1)
json = "{\"cmd\":1023,\"operation\":1,\"result\":0}";
else
json = "{\"cmd\":1023,\"operation\":2,\"result\":0}";
break;
}
case 1024:{
int operation = -1;
if(json_object_object_get_ex(j_info, "operation", &_value))
{
operation = json_object_get_int(_value);
}
if(operation == 1)
json = "{\"cmd\":1024,\"operation\":1,\"result\":0}";
else
json = "{\"cmd\":1024,\"operation\":2,\"result\":0}";
break;
}
case 1025:{
int operation = -1;
if(json_object_object_get_ex(j_info, "operation", &_value))
{
operation = json_object_get_int(_value);
}
if(operation == 1)
json = "{\"cmd\":1025,\"operation\":1,\"result\":0}";
else
json = "{\"cmd\":1025,\"operation\":2,\"result\":0}";
break;
}
case 1026:
json = "{\"cmd\":1026,\"result\":0}";
break;
case 1027:
json = "{\"cmd\":1027,\"result\":0}";
break;
case 1028:
json = "{\"cmd\":1028,\"result\":0}";
break;
case 1029:
json = "{\"cmd\":1029,\"result\":0,\"distance\":5.5,\"nlonlat\":\"116.39968,39.90145\",\"nm\":\"大连服务站\"}";
break;
case 1031:
json = "{\"cmd\":1031,\"result\":0,\"current_location\":{\"name\":\"高新园区腾飞软件园6期\"}}";
break;
case 1032:{
int operation = -1;
if(json_object_object_get_ex(j_info, "operation", &_value))
{
operation = json_object_get_int(_value);
}
if(operation == 1)
json = "{\"cmd\":1032,\"operation\":1,\"result\":0}";
else
json = "{\"cmd\":1032,\"operation\":2,\"result\":0}";
break;
}
case 1033:
json = "{\"cmd\":1033,\"result\":0}";
break;
case 1034:{
int operation = -1;
if(json_object_object_get_ex(j_info, "operation", &_value))
{
operation = json_object_get_int(_value);
}
if(operation == 1)
json = "{\"cmd\":1034,\"operation\":1,\"result\":0}";
else
json = "{\"cmd\":1034,\"operation\":2,\"result\":0}";
break;
}
case 1035:
json = "{\"cmd\":1035,\"result\":0,\"distance\":5.5,\"time\":15}";
break;
case 1036:
json = "{\"cmd\":1036,\"result\":0,\"expense\":80}";
break;
case 1037:
json = "{\"cmd\":1037,\"result\":0}";
break;
case 1038:
json = "{\"cmd\":1038,\"result\":0}";
break;
case 2001:
json = "{\"cmd\":2001,\"result\":1}";
break;
}
Navi_send_to_VR(NAVI2VR_RESPONSE_T,json);
}
int main()
{
while(Navi_connect_VR(recv_from_VR_callback) != 0){
sleep(1);
}
Navi_send_to_VR(NAVI2VR_NOTIFY_T,"{\"cmd\":2001,\"result\":1}");
while(1){
usleep(1000);
}
}
COMM.h
#ifndef SERVER_CLIENT_API_H_
#define SERVER_CLIENT_API_H_
typedef enum{
VR2NAVI_REQUEST_T,
NAVI2VR_RESPONSE_T,
NAVI2VR_NOTIFY_T,
NAVI2VR_REQUEST_T,
VR2NAVI_RESPONSE_T,
VR2NAVI_NOTIFY_T,
}COMM_TYPE;
#define SOCKET_ADDRESS "/tmp/VR_Navi_socket"
#ifdef __cplusplus
extern "C" {
#endif
typedef void (*recv_callback_t)(int type, char * filename);
int VR_create_server(recv_callback_t callback);
int VR_send_to_navi(int type, const char * json);
int Navi_connect_VR(recv_callback_t callback);
int Navi_send_to_VR(int type, const char * json);
#ifdef __cplusplus
};
#endif
#endif