socket_demo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include "ql_api_osi.h"
  17. #include "ql_api_nw.h"
  18. #include "ql_log.h"
  19. #include "ql_api_datacall.h"
  20. #include "sockets.h"
  21. #include "lwip/ip_addr.h"
  22. #include "lwip/ip6_addr.h"
  23. #include "lwip/netdb.h"
  24. #include "lwip/netif.h"
  25. #include "lwip/inet.h"
  26. #include "lwip/tcp.h"
  27. #include "ql_ssl.h"
  28. #include "ip4_addr.h"
  29. #include "inet.h"
  30. #include "ntp_info_decode.h"
  31. #include "ql_api_dev.h"
  32. #include "ql_api_sim.h"
  33. #define QL_SOCKET_LOG_LEVEL QL_LOG_LEVEL_INFO
  34. #define QL_SOCKET_LOG(msg, ...) QL_LOG(QL_SOCKET_LOG_LEVEL, "ql_Sock", msg, ##__VA_ARGS__)
  35. #define QL_SOCKET_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_SSL", msg, ##__VA_ARGS__)
  36. #define QL_SOCKET_DEMO_CHANGE_DEMO 0
  37. #if QL_SOCKET_DEMO_CHANGE_DEMO
  38. #define QL_SOCKET_DEMO_URL "www.baidu.com"
  39. #else
  40. #define QL_SOCKET_DEMO_URL "nas.fast-fun.cn"//"220.180.239.212"
  41. #endif
  42. /*
  43. #define SERVER_PORT 8085
  44. #define SERVER_IP "47.107.99.203"
  45. #define SERVER_URL "nas.fast-fun.cn"//"220.180.239.212"
  46. */
  47. uint8_t nSim = 0;
  48. int profile_idx = 1;
  49. static void mt(void);
  50. u32_t my_ipaddr_addr(const char *cp)
  51. {
  52. ip4_addr_t val;
  53. if (ip4addr_aton(cp, &val)) {
  54. return ip4_addr_get_u32(&val);
  55. }
  56. return (IPADDR_NONE);
  57. }
  58. static ql_task_t socket_demo_task = NULL;
  59. static char send_buf[128]={0};
  60. static int send_len = 0;
  61. static char recv_buf[128]={0};
  62. static int recv_len = 0;
  63. //获取socket接收缓存的剩余大小
  64. int ql_socket_get_free_size(int s,void *value)
  65. {
  66. int ret = ERR_OK;
  67. if (s < 0||value==NULL)
  68. {
  69. return -1;
  70. }
  71. *((int32_t *)value) =lwip_getRecvBufSize(s)-lwip_getRecvAvailSize(s);
  72. QL_SOCKET_LOG("socket free size:%d", *((int32_t *)value));
  73. if(*((int32_t *)value)<0)
  74. {
  75. ret = ERR_VAL;
  76. }
  77. return ret;
  78. }
  79. //获取socket发送缓存中已发送但未收到回应的数据大小
  80. int ql_socket_get_unacked_size(int s,void *value)
  81. {
  82. int ret = ERR_OK;
  83. if (s < 0||value==NULL)
  84. {
  85. return -1;
  86. }
  87. *((int32_t *)value) =lwip_getSentSize(s)-lwip_getAckedSize(s);
  88. QL_SOCKET_LOG("socket unacked size:%d", *((int32_t *)value));
  89. if(*((int32_t *)value)<0)
  90. {
  91. ret = ERR_VAL;
  92. }
  93. return ret;
  94. }
  95. #if 1
  96. static void socket_app_thread(void * arg)
  97. {
  98. int ret = 0;
  99. int i = 0;
  100. ql_data_call_info_s info;
  101. char ip4_addr_str[16] = {0};
  102. fd_set read_fds;
  103. fd_set write_fds;
  104. fd_set exp_fds;
  105. int socket_fd = -1;
  106. int flags = 0;
  107. int fd_changed = 0;
  108. int connected = 0;
  109. int closing = false;
  110. struct sockaddr_in local4, server_ipv4;
  111. struct addrinfo *pres = NULL;
  112. struct addrinfo *temp = NULL;
  113. ql_nw_data_count_info_s data_count_info = {0};
  114. #if QL_SOCKET_DEMO_CHANGE_DEMO
  115. ql_datacall_dns_info_s dns_pri = {0};
  116. ql_datacall_dns_info_s dns_sec = {0};
  117. #endif
  118. ql_rtos_task_sleep_s(10);
  119. QL_SOCKET_LOG("========== socket demo start ==========");
  120. QL_SOCKET_LOG("wait for network register done");
  121. while((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10){
  122. i++;
  123. ql_rtos_task_sleep_s(1);
  124. }
  125. if(ret == 0){
  126. i = 0;
  127. QL_SOCKET_LOG("====network registered!!!!====");
  128. }else{
  129. QL_SOCKET_LOG("====network register failure!!!!!====");
  130. goto exit;
  131. }
  132. ql_set_data_call_asyn_mode(nSim, profile_idx, 0);
  133. QL_SOCKET_LOG("===start data call====");
  134. ret=ql_start_data_call(nSim, profile_idx, QL_PDP_TYPE_IP, "cmnet", NULL, NULL, 0);
  135. QL_SOCKET_LOG("===data call result:%d", ret);
  136. if(ret != 0){
  137. QL_SOCKET_LOG("====data call failure!!!!=====");
  138. }
  139. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  140. ret = ql_get_data_call_info(nSim, profile_idx, &info);
  141. if(ret != 0){
  142. QL_SOCKET_LOG("ql_get_data_call_info ret: %d", ret);
  143. ql_stop_data_call(nSim, profile_idx);
  144. goto exit;
  145. }
  146. QL_SOCKET_LOG("info->profile_idx: %d", info.profile_idx);
  147. QL_SOCKET_LOG("info->ip_version: %d", info.ip_version);
  148. QL_SOCKET_LOG("info->v4.state: %d", info.v4.state);
  149. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  150. QL_SOCKET_LOG("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  151. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  152. QL_SOCKET_LOG("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  153. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  154. QL_SOCKET_LOG("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  155. #if QL_SOCKET_DEMO_CHANGE_DEMO
  156. ql_datacall_get_dns_addr(nSim, profile_idx, &dns_pri, &dns_sec);
  157. QL_SOCKET_LOG("get_dns_pri:[%d][%s]", dns_pri.type, ip4addr_ntoa(&dns_pri.ip4));
  158. QL_SOCKET_LOG("get_dns_sec:[%d][%s]", dns_sec.type, ip4addr_ntoa(&dns_sec.ip4));
  159. memset(&dns_pri, 0x00, sizeof(ql_datacall_dns_info_s));
  160. dns_pri.type = QL_PDP_TYPE_IP;
  161. dns_sec.type = QL_PDP_TYPE_IP;
  162. ip4addr_aton("114.114.114.114", &(dns_pri.ip4));
  163. ip4addr_aton("8.8.8.8", &(dns_sec.ip4));
  164. ql_datacall_set_dns_addr(nSim, profile_idx, &dns_pri, &dns_sec);
  165. memset(&dns_pri, 0x00, sizeof(ql_datacall_dns_info_s));
  166. ql_datacall_get_dns_addr(nSim, profile_idx, &dns_pri, &dns_sec);
  167. QL_SOCKET_LOG("get_dns_pri:[%d][%s]", dns_pri.type, ip4addr_ntoa(&dns_pri.ip4));
  168. QL_SOCKET_LOG("get_dns_sec:[%d][%s]", dns_sec.type, ip4addr_ntoa(&dns_sec.ip4));
  169. #endif
  170. memset(&local4, 0x00, sizeof(struct sockaddr_in));
  171. local4.sin_family = AF_INET;
  172. local4.sin_port = 0;
  173. inet_aton(ip4addr_ntoa(&info.v4.addr.ip), &local4.sin_addr);
  174. loop:
  175. ret = getaddrinfo_with_pcid(QL_SOCKET_DEMO_URL, NULL, NULL, &pres, (uint32)profile_idx);
  176. if (ret < 0 || pres == NULL)
  177. {
  178. QL_SOCKET_LOG("DNS getaddrinfo failed! ret=%d; pres=%p!\n",ret,pres);
  179. goto exit;
  180. }
  181. FD_ZERO(&read_fds);
  182. FD_ZERO(&write_fds);
  183. FD_ZERO(&exp_fds);
  184. flags = 0;
  185. connected = 0;
  186. closing = false;
  187. i = 0;
  188. for(temp = pres; temp != NULL; temp = temp->ai_next)
  189. {
  190. //struct sockaddr_in * sin_res = (struct sockaddr_in *)temp->ai_addr;
  191. if(temp->ai_family == AF_INET){
  192. socket_fd = socket(temp->ai_family, SOCK_STREAM, 0);
  193. if(socket_fd < 0){
  194. continue;
  195. }
  196. ret = bind(socket_fd,(struct sockaddr *)&local4,sizeof(struct sockaddr));
  197. if(ret < 0){
  198. close(socket_fd);
  199. socket_fd = -1;
  200. continue;
  201. }
  202. flags |= O_NONBLOCK;
  203. fcntl(socket_fd, F_SETFL,flags);
  204. memset(&server_ipv4, 0x00, sizeof(struct sockaddr_in));
  205. server_ipv4.sin_family = AF_INET;
  206. server_ipv4.sin_port = htons(7780);
  207. server_ipv4.sin_addr.s_addr = my_ipaddr_addr("47.107.99.203");
  208. ret = connect(socket_fd, (struct sockaddr *)&server_ipv4, sizeof(struct sockaddr_in));
  209. if(ret == 0){
  210. connected = 1;
  211. break;
  212. }else{
  213. if(lwip_get_error(socket_fd) != EINPROGRESS){
  214. close(socket_fd);
  215. socket_fd = -1;
  216. continue;
  217. }else{
  218. break;
  219. }
  220. }
  221. }
  222. }
  223. if(socket_fd < 0){
  224. goto exit;
  225. }
  226. if(connected == 1){
  227. FD_SET(socket_fd, &read_fds);
  228. QL_SOCKET_LOG("=====connect to my_server success=====");
  229. // memset(send_buf, 0x00, 128);
  230. // send_len = snprintf(send_buf, 128,"%d%s%d\r\n",i,"startAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAend",i);
  231. // if(write(socket_fd, send_buf, send_len) != send_len){
  232. // FD_SET(socket_fd, &write_fds);
  233. // }else{
  234. // i++;
  235. // }
  236. }else
  237. {
  238. FD_SET(socket_fd, &write_fds);
  239. }
  240. FD_SET(socket_fd, &exp_fds);
  241. char devinfo[64] = {0};
  242. ret = ql_dev_get_imei(devinfo, 64, 0);
  243. QL_SOCKET_LOG("devinfo:");
  244. hc_flash_data_printf((unsigned char *)devinfo, 64);
  245. uint8_t sim_id = 0;
  246. char siminfo[64] = {0};
  247. QL_SOCKET_LOG("siminfo:");
  248. hc_flash_data_printf((unsigned char *)siminfo, 64);
  249. ret = ql_sim_get_iccid(sim_id, siminfo, 64);
  250. npt_func_get_info(nSim, profile_idx);
  251. mt();
  252. while(1)
  253. {
  254. fd_changed = select(socket_fd+1, &read_fds, &write_fds, &exp_fds, NULL);
  255. if(fd_changed > 0)
  256. {
  257. if(FD_ISSET(socket_fd, &write_fds))
  258. {
  259. FD_CLR(socket_fd, &write_fds);
  260. if(connected== 0)
  261. {
  262. int value = 0;
  263. int len = 0;
  264. len = sizeof(value);
  265. getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, &value, &len);
  266. QL_SOCKET_LOG("errno: %d", value);
  267. if(value == 0 || value == EISCONN )
  268. {
  269. QL_SOCKET_LOG("=====connect to \"220.180.239.212:8252\" success=====");
  270. connected = 1;
  271. FD_SET(socket_fd, &read_fds);
  272. memset(send_buf, 0x00, 128);
  273. send_len = snprintf(send_buf, 128,"%d%s%d\r\n",i,"startAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAend",i);
  274. write(socket_fd, send_buf, send_len);
  275. i++;
  276. }else
  277. {
  278. QL_SOCKET_LOG("=====connect to \"220.180.239.212:8252\" failed=====");
  279. close(socket_fd);
  280. socket_fd = -1;
  281. goto reboot;
  282. }
  283. }else
  284. {
  285. memset(send_buf, 0x00, 128);
  286. send_len = snprintf(send_buf, 128,"%d%s%d\r\n",i,"startAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAend",i);
  287. write(socket_fd, send_buf, send_len);
  288. i++;
  289. }
  290. }else if(FD_ISSET(socket_fd, &read_fds))
  291. {
  292. FD_CLR(socket_fd, &read_fds);
  293. memset(recv_buf,0x00, 128);
  294. recv_len = read(socket_fd, recv_buf, 128);
  295. if(recv_len > 0)
  296. {
  297. for(int i = 0;i < recv_len;i++)
  298. QL_SOCKET_LOG(">>>>Recv: %x \n", recv_buf[i]);
  299. QL_SOCKET_LOG("chengwei recv ok\n");
  300. memset(send_buf, 0x00, 128);
  301. send_len = snprintf(send_buf, 128,"%d%s%d\r\n",i,"startAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAend",i);
  302. write(socket_fd, send_buf, send_len);
  303. i++;
  304. FD_SET(socket_fd, &read_fds);
  305. if(ql_nw_get_data_count(nSim, &data_count_info) == 0)
  306. {
  307. QL_SOCKET_LOG("data count: uplink %llu, downlink %llu", data_count_info.uplink_data_count, data_count_info.downlink_data_count);
  308. }
  309. }else if(recv_len == 0)
  310. {
  311. if(closing == false)
  312. {
  313. QL_SOCKET_LOG("===passive close!!!!");
  314. shutdown(socket_fd, SHUT_WR);
  315. closing = true;
  316. FD_SET(socket_fd, &read_fds);
  317. }else
  318. {
  319. close(socket_fd);
  320. socket_fd = -1;
  321. goto reboot;
  322. }
  323. }else
  324. {
  325. if(lwip_get_error(socket_fd) == EAGAIN)
  326. {
  327. FD_SET(socket_fd, &read_fds);
  328. }else
  329. {
  330. close(socket_fd);
  331. socket_fd = -1;
  332. goto reboot;
  333. }
  334. }
  335. }else if(FD_ISSET(socket_fd, &exp_fds))
  336. {
  337. FD_CLR(socket_fd, &exp_fds);
  338. close(socket_fd);
  339. socket_fd = -1;
  340. goto reboot;
  341. }
  342. }
  343. reboot:
  344. if(socket_fd < 0)
  345. {
  346. }
  347. }
  348. if(pres!=NULL)
  349. {
  350. freeaddrinfo(pres);
  351. pres = NULL;
  352. }
  353. goto loop;
  354. exit:
  355. ql_rtos_task_delete(socket_demo_task);
  356. return;
  357. }
  358. #endif
  359. int ql_socket_app_init(void)
  360. {
  361. QlOSStatus err = QL_OSI_SUCCESS;
  362. err = ql_rtos_task_create(&socket_demo_task, 16*1024, APP_PRIORITY_ABOVE_NORMAL, "QsocketApp", socket_app_thread, NULL, 5);
  363. if(err != QL_OSI_SUCCESS)
  364. {
  365. QL_SOCKET_LOG("socket_app init failed");
  366. }
  367. return err;
  368. }
  369. /*
  370. static UTC8TimeType UTC8TimeTcp;
  371. static void vTimer1000msCallback(TimerHandle_t pxTimer)
  372. {
  373. uint32 ulTimerID;
  374. ulTimerID = (uint32)pvTimerGetTimerID(pxTimer);
  375. sint16 *TcpDataFeqPtr =NULL;
  376. TcpDataFeqPtr = (sint16 *)&TcpDataFeq;
  377. if (ulTimerID == 0)
  378. {
  379. if(SocketId>=0)
  380. {
  381. for(uint8 i=2;i<sizeof(TcpDataFeq)/sizeof(sint16);i=i+3)
  382. {
  383. *(TcpDataFeqPtr+i) = (*(TcpDataFeqPtr+i)-1)<0 ? 0 : (*(TcpDataFeqPtr+i)-1);
  384. }
  385. }
  386. TimeUpdate(&UTC8TimeTcp);
  387. }
  388. }
  389. */
  390. //osiTimer_t *osiTimerCreate(osiThread_t *thread, osiCallback_t cb, void *ctx);
  391. //bool osiTimerSetPeriod(osiTimer_t *timer, uint32_t ms, bool periodic);
  392. //bool osiTimerSetPeriodRelaxed(osiTimer_t *timer, uint32_t ms, uint32_t relaxed_ms, bool periodic);
  393. static uint32_t run_num = 0;
  394. static osiTimer_t *timer1=NULL;
  395. static void timer1_callback_function(void)
  396. {
  397. run_num++;
  398. QL_SOCKET_LOG("creat my timer ok %d\n", run_num);
  399. osiTimerStartRelaxed(timer1, 1000, OSI_WAIT_FOREVER);
  400. }
  401. static void mt(void)
  402. {
  403. //osiThread_t *thread = osiThreadCurrent();
  404. timer1 = osiTimerCreate(NULL,(osiCallback_t)timer1_callback_function, NULL);
  405. if(timer1!=NULL)
  406. QL_SOCKET_LOG("creat my timer ok\n");
  407. else
  408. QL_SOCKET_LOG("creat my timer err\n");
  409. bool ret = osiTimerStart(timer1, 0);
  410. if(ret ==true)
  411. QL_SOCKET_LOG("start my timer ok\n");
  412. else
  413. QL_SOCKET_LOG("start my timer err\n");
  414. }/**/