test.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "fcgi_stdio.h"
  2. #include "../include/YT_EHOME_shm.h"
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <json-c/json.h>
  6. #include <ctype.h>
  7. MyEHomeShm *ytShm;
  8. unsigned char* urldecode(char *string)
  9. {
  10. int destlen = 0;
  11. unsigned char *src, *dest;
  12. unsigned char *newstr;
  13. if (string == NULL) return NULL;
  14. for (src = (unsigned char *)string; *src != '\0'; src++)
  15. {
  16. if (*src == '%')
  17. {
  18. destlen++;
  19. }
  20. }
  21. newstr = (unsigned char *)malloc(destlen + 1);
  22. src = (unsigned char *)string;
  23. dest = newstr;
  24. while (*src != '\0')
  25. {
  26. if (*src == '%')
  27. {
  28. char h = toupper(src[1]);
  29. char l = toupper(src[2]);
  30. int vh, vl;
  31. vh = isalpha(h) ? (10+(h-'A')) : (h-'0');
  32. vl = isalpha(l) ? (10+(l-'A')) : (l-'0');
  33. *dest++ = ((vh<<4)+vl);
  34. src += 3;
  35. } else if (*src == '+') {
  36. *dest++ = ' ';
  37. src++;
  38. } else {
  39. *dest++ = *src++;
  40. }
  41. }
  42. *dest = 0;
  43. return newstr;
  44. }
  45. bool shm_load()
  46. {
  47. int shmid;
  48. key_t key;
  49. if((key = ftok(SHM_PATH,(int)SHM_PORT))==-1)
  50. return false;
  51. if((shmid = shmget(key,sizeof(MyEHomeShm),IPC_CREAT|0666))==-1)
  52. return false;
  53. ytShm = (MyEHomeShm *)shmat(shmid,NULL,0);
  54. return true;
  55. }
  56. json_object *get_hc_list(){
  57. int i;
  58. json_object *obj;
  59. json_object *list = json_object_new_array();
  60. char tmp[256];
  61. for(i=0;i<DEVICES_COUNT;i++){
  62. if(ytShm->eHomeDevice[i].io[8]!=0x00)
  63. {
  64. obj = json_object_new_object();
  65. sprintf(tmp,"%s",ytShm->eHomeDevice[i].deviceID);
  66. json_object_object_add(obj,"ID",json_object_new_int(i+1));
  67. json_object_object_add(obj,"DEVICE",json_object_new_string((const char *)tmp));
  68. if(ytShm->eHomeDevice[i].io[8]==0x01)
  69. json_object_object_add(obj,"LIVEONLINE",json_object_new_boolean(true));
  70. else
  71. json_object_object_add(obj,"LIVEONLINE",json_object_new_boolean(false));
  72. if(ytShm->eHomeDevice[i].transed==0x01)
  73. json_object_object_add(obj,"ONAIR",json_object_new_boolean(true));
  74. else
  75. json_object_object_add(obj,"ONAIR",json_object_new_boolean(false));
  76. json_object_object_add(obj,"ALIVEVALUE",json_object_new_int(ytShm->eHomeDevice[i].io[14]&0xff));
  77. json_object_array_add(list,obj);
  78. }
  79. }
  80. return list;
  81. }
  82. json_object *get_cu_list(){
  83. int i;
  84. json_object *obj;
  85. json_object *list = json_object_new_array();
  86. char tmp[256];
  87. for(i=0;i<DEVICES_COUNT;i++){
  88. if(ytShm->eHomeDevice[i].Connected==0x01){
  89. obj = json_object_new_object();
  90. sprintf(tmp,"%s",ytShm->eHomeDevice[i].deviceID);
  91. json_object_object_add(obj,"ID",json_object_new_int(i+1));
  92. json_object_object_add(obj,"DEVICE",json_object_new_string((const char *)tmp));
  93. json_object_array_add(list,obj);
  94. }
  95. }
  96. }
  97. bool active_live(json_object *q_body){
  98. json_object *q_device=NULL, *q_act=NULL, *q_idx=NULL;
  99. int act=-1,idx=0,i;
  100. char deviceId[256],tmp[256];
  101. json_object_object_foreach(q_body,key,val){
  102. if(strcmp(key,"DEVICE")==0)
  103. q_device = val;
  104. else if(strcmp(key,"ACTION")==0)
  105. q_act = val;
  106. else if(strcmp(key,"IDX")==0){
  107. q_idx = val;
  108. if(json_object_get_type(q_idx)==json_type_int)
  109. idx = json_object_get_int(q_idx);
  110. }
  111. }
  112. if((NULL!=q_device)&&(NULL!=q_act)&&(json_object_get_type(q_device)==json_type_string)&&(json_object_get_type(q_act)==json_type_int)){
  113. sprintf(deviceId,"%s",json_object_get_string(q_device));
  114. act = json_object_get_int(q_act);
  115. if((act==0x00)||(act==0x01)){
  116. for(i=0;i<DEVICES_COUNT;i++){
  117. if(ytShm->eHomeDevice[i].Inited==0x01){
  118. // sprintf(tmp,"%s",ytShm->eHomeDevice[i].deviceID);
  119. if(strcmp(ytShm->eHomeDevice[i].deviceID,deviceId)==0){
  120. if((idx>=0)&&(idx<8)){
  121. ytShm->eHomeDevice[i].io[idx]=act;
  122. return true;
  123. }else
  124. return false;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. return false;
  131. }
  132. bool json_request_action(char *request, json_object *body){
  133. json_object *q_json = NULL, *q_body =NULL, *q_cmd = NULL;
  134. int cmd;
  135. q_json = json_tokener_parse(request);
  136. if(q_json!=NULL){
  137. // json_object_object_add(body,"QUERY",q_json);
  138. json_object_object_foreach(q_json,key,val){
  139. if(strcmp(key,"CMD")==0)
  140. q_cmd = val;
  141. else if(strcmp(key,"BODY")==0)
  142. q_body = val;
  143. }
  144. if(NULL!=q_cmd){
  145. if(json_object_get_type(q_cmd)==json_type_int){
  146. cmd = json_object_get_int(q_cmd);
  147. switch (cmd) {
  148. case GET_LIVE_LIST:
  149. json_object_object_add(body,"CMD",json_object_new_int(cmd));
  150. json_object_object_add(body,"LIST",get_hc_list());
  151. break;
  152. case GET_ONLINE_LIST:
  153. json_object_object_add(body,"CMD",json_object_new_int(cmd));
  154. json_object_object_add(body,"LIST",get_cu_list());
  155. break;
  156. case ACTIVE_LIVE:
  157. json_object_object_add(body,"CMD",json_object_new_int(cmd));
  158. if(active_live(q_body))
  159. json_object_object_add(body,"ACTION",json_object_new_int(1));
  160. else
  161. json_object_object_add(body,"ACTION",json_object_new_int(0));
  162. break;
  163. default:
  164. json_object_object_add(body,"RESULT",json_object_new_int(0));
  165. break;
  166. }
  167. }
  168. }else{
  169. json_object_put(q_json);
  170. return false;
  171. }
  172. json_object_put(q_json);
  173. }else
  174. return false;
  175. return true;
  176. }
  177. int main(void)
  178. {
  179. int i;
  180. char input[1024],lenstr[60],data[1024];
  181. int len;
  182. json_object *json,*json_body;
  183. if(shm_load()){
  184. while (FCGI_Accept() >= 0){
  185. sprintf(lenstr,"%s",getenv("CONTENT_LENGTH"));
  186. len = atoi(lenstr);
  187. if((len>0)&&(len<1024)){
  188. bzero(input,sizeof(input));
  189. bzero(data,sizeof(data));
  190. fgets(input,len+1,stdin);
  191. for(i=0;i<(len-10);i++)
  192. data[i] = input[10+i];
  193. json = json_object_new_object();
  194. json_body = json_object_new_object();
  195. if(json_request_action(data,json_body)){
  196. json_object_object_add(json,"RESULT",json_body);
  197. json_object_object_add(json,"REPLY",json_object_new_int(1));
  198. }else{
  199. json_object_object_add(json,"REPLY",json_object_new_int(0));
  200. }
  201. json_object_object_add(json,"QUERY",json_object_new_string(data));
  202. printf("Access-Control-Allow-Origin: *\r\n");
  203. printf("Content-type: text/html\r\n\r\n%s",json_object_to_json_string(json));
  204. json_object_put(json);
  205. }
  206. }
  207. shmdt(ytShm);
  208. }
  209. return 0;
  210. }