123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- /*================================================================
- Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
- Quectel Wireless Solution Proprietary and Confidential.
- =================================================================*/
- /*=================================================================
- EDIT HISTORY FOR MODULE
- This section contains comments describing changes made to the module.
- Notice that changes are listed in reverse chronological order.
- WHEN WHO WHAT, WHERE, WHY
- ------------ ------- -------------------------------------------------------------------------------
- =================================================================*/
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "ql_api_osi.h"
- #include "ql_api_nw.h"
- #include "ql_log.h"
- #include "ql_api_datacall.h"
- #include "sockets.h"
- #include "lwip/ip_addr.h"
- #include "lwip/ip6_addr.h"
- #include "lwip/netdb.h"
- #include "lwip/netif.h"
- #include "lwip/inet.h"
- #include "lwip/tcp.h"
- #include "ql_ssl.h"
- #include "ip4_addr.h"
- #include "inet.h"
- #include "ntp_info_decode.h"
- #include "ql_api_dev.h"
- #include "ql_api_sim.h"
- #define QL_SOCKET_LOG_LEVEL QL_LOG_LEVEL_INFO
- #define QL_SOCKET_LOG(msg, ...) QL_LOG(QL_SOCKET_LOG_LEVEL, "ql_Sock", msg, ##__VA_ARGS__)
- #define QL_SOCKET_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_SSL", msg, ##__VA_ARGS__)
- #define QL_SOCKET_DEMO_CHANGE_DEMO 0
- #if QL_SOCKET_DEMO_CHANGE_DEMO
- #define QL_SOCKET_DEMO_URL "www.baidu.com"
- #else
- #define QL_SOCKET_DEMO_URL "nas.fast-fun.cn"//"220.180.239.212"
- #endif
- /*
- #define SERVER_PORT 8085
- #define SERVER_IP "47.107.99.203"
- #define SERVER_URL "nas.fast-fun.cn"//"220.180.239.212"
- */
- uint8_t nSim = 0;
- int profile_idx = 1;
- static void mt(void);
- u32_t my_ipaddr_addr(const char *cp)
- {
- ip4_addr_t val;
- if (ip4addr_aton(cp, &val)) {
- return ip4_addr_get_u32(&val);
- }
- return (IPADDR_NONE);
- }
-
- static ql_task_t socket_demo_task = NULL;
- static char send_buf[128]={0};
- static int send_len = 0;
- static char recv_buf[128]={0};
- static int recv_len = 0;
- //获取socket接收缓存的剩余大小
- int ql_socket_get_free_size(int s,void *value)
- {
- int ret = ERR_OK;
- if (s < 0||value==NULL)
- {
- return -1;
- }
- *((int32_t *)value) =lwip_getRecvBufSize(s)-lwip_getRecvAvailSize(s);
- QL_SOCKET_LOG("socket free size:%d", *((int32_t *)value));
- if(*((int32_t *)value)<0)
- {
- ret = ERR_VAL;
- }
- return ret;
- }
- //获取socket发送缓存中已发送但未收到回应的数据大小
- int ql_socket_get_unacked_size(int s,void *value)
- {
- int ret = ERR_OK;
- if (s < 0||value==NULL)
- {
- return -1;
- }
- *((int32_t *)value) =lwip_getSentSize(s)-lwip_getAckedSize(s);
- QL_SOCKET_LOG("socket unacked size:%d", *((int32_t *)value));
- if(*((int32_t *)value)<0)
- {
- ret = ERR_VAL;
- }
- return ret;
- }
- #if 1
- static void socket_app_thread(void * arg)
- {
- int ret = 0;
- int i = 0;
-
- ql_data_call_info_s info;
- char ip4_addr_str[16] = {0};
- fd_set read_fds;
- fd_set write_fds;
- fd_set exp_fds;
- int socket_fd = -1;
- int flags = 0;
- int fd_changed = 0;
- int connected = 0;
- int closing = false;
- struct sockaddr_in local4, server_ipv4;
- struct addrinfo *pres = NULL;
- struct addrinfo *temp = NULL;
-
- ql_nw_data_count_info_s data_count_info = {0};
- #if QL_SOCKET_DEMO_CHANGE_DEMO
- ql_datacall_dns_info_s dns_pri = {0};
- ql_datacall_dns_info_s dns_sec = {0};
- #endif
- ql_rtos_task_sleep_s(10);
- QL_SOCKET_LOG("========== socket demo start ==========");
- QL_SOCKET_LOG("wait for network register done");
-
- while((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10){
- i++;
- ql_rtos_task_sleep_s(1);
- }
- if(ret == 0){
- i = 0;
- QL_SOCKET_LOG("====network registered!!!!====");
- }else{
- QL_SOCKET_LOG("====network register failure!!!!!====");
- goto exit;
- }
- ql_set_data_call_asyn_mode(nSim, profile_idx, 0);
- QL_SOCKET_LOG("===start data call====");
- ret=ql_start_data_call(nSim, profile_idx, QL_PDP_TYPE_IP, "cmnet", NULL, NULL, 0);
- QL_SOCKET_LOG("===data call result:%d", ret);
- if(ret != 0){
- QL_SOCKET_LOG("====data call failure!!!!=====");
- }
- memset(&info, 0x00, sizeof(ql_data_call_info_s));
-
- ret = ql_get_data_call_info(nSim, profile_idx, &info);
- if(ret != 0){
- QL_SOCKET_LOG("ql_get_data_call_info ret: %d", ret);
- ql_stop_data_call(nSim, profile_idx);
- goto exit;
- }
- QL_SOCKET_LOG("info->profile_idx: %d", info.profile_idx);
- QL_SOCKET_LOG("info->ip_version: %d", info.ip_version);
-
- QL_SOCKET_LOG("info->v4.state: %d", info.v4.state);
- inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
- QL_SOCKET_LOG("info.v4.addr.ip: %s\r\n", ip4_addr_str);
- inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
- QL_SOCKET_LOG("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
- inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
- QL_SOCKET_LOG("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
- #if QL_SOCKET_DEMO_CHANGE_DEMO
- ql_datacall_get_dns_addr(nSim, profile_idx, &dns_pri, &dns_sec);
- QL_SOCKET_LOG("get_dns_pri:[%d][%s]", dns_pri.type, ip4addr_ntoa(&dns_pri.ip4));
- QL_SOCKET_LOG("get_dns_sec:[%d][%s]", dns_sec.type, ip4addr_ntoa(&dns_sec.ip4));
- memset(&dns_pri, 0x00, sizeof(ql_datacall_dns_info_s));
- dns_pri.type = QL_PDP_TYPE_IP;
- dns_sec.type = QL_PDP_TYPE_IP;
- ip4addr_aton("114.114.114.114", &(dns_pri.ip4));
- ip4addr_aton("8.8.8.8", &(dns_sec.ip4));
- ql_datacall_set_dns_addr(nSim, profile_idx, &dns_pri, &dns_sec);
- memset(&dns_pri, 0x00, sizeof(ql_datacall_dns_info_s));
- ql_datacall_get_dns_addr(nSim, profile_idx, &dns_pri, &dns_sec);
- QL_SOCKET_LOG("get_dns_pri:[%d][%s]", dns_pri.type, ip4addr_ntoa(&dns_pri.ip4));
- QL_SOCKET_LOG("get_dns_sec:[%d][%s]", dns_sec.type, ip4addr_ntoa(&dns_sec.ip4));
- #endif
- memset(&local4, 0x00, sizeof(struct sockaddr_in));
- local4.sin_family = AF_INET;
- local4.sin_port = 0;
- inet_aton(ip4addr_ntoa(&info.v4.addr.ip), &local4.sin_addr);
- loop:
- ret = getaddrinfo_with_pcid(QL_SOCKET_DEMO_URL, NULL, NULL, &pres, (uint32)profile_idx);
- if (ret < 0 || pres == NULL)
- {
- QL_SOCKET_LOG("DNS getaddrinfo failed! ret=%d; pres=%p!\n",ret,pres);
- goto exit;
- }
- FD_ZERO(&read_fds);
- FD_ZERO(&write_fds);
- FD_ZERO(&exp_fds);
- flags = 0;
- connected = 0;
- closing = false;
- i = 0;
- for(temp = pres; temp != NULL; temp = temp->ai_next)
- {
- //struct sockaddr_in * sin_res = (struct sockaddr_in *)temp->ai_addr;
- if(temp->ai_family == AF_INET){
- socket_fd = socket(temp->ai_family, SOCK_STREAM, 0);
- if(socket_fd < 0){
- continue;
- }
- ret = bind(socket_fd,(struct sockaddr *)&local4,sizeof(struct sockaddr));
- if(ret < 0){
- close(socket_fd);
- socket_fd = -1;
- continue;
- }
-
- flags |= O_NONBLOCK;
- fcntl(socket_fd, F_SETFL,flags);
- memset(&server_ipv4, 0x00, sizeof(struct sockaddr_in));
- server_ipv4.sin_family = AF_INET;
- server_ipv4.sin_port = htons(7780);
- server_ipv4.sin_addr.s_addr = my_ipaddr_addr("47.107.99.203");
- ret = connect(socket_fd, (struct sockaddr *)&server_ipv4, sizeof(struct sockaddr_in));
-
- if(ret == 0){
- connected = 1;
- break;
- }else{
- if(lwip_get_error(socket_fd) != EINPROGRESS){
- close(socket_fd);
- socket_fd = -1;
- continue;
- }else{
- break;
- }
- }
- }
- }
- if(socket_fd < 0){
- goto exit;
- }
- if(connected == 1){
- FD_SET(socket_fd, &read_fds);
- QL_SOCKET_LOG("=====connect to my_server success=====");
- // memset(send_buf, 0x00, 128);
- // send_len = snprintf(send_buf, 128,"%d%s%d\r\n",i,"startAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAend",i);
-
- // if(write(socket_fd, send_buf, send_len) != send_len){
- // FD_SET(socket_fd, &write_fds);
- // }else{
- // i++;
- // }
- }else
- {
- FD_SET(socket_fd, &write_fds);
- }
- FD_SET(socket_fd, &exp_fds);
- char devinfo[64] = {0};
- ret = ql_dev_get_imei(devinfo, 64, 0);
-
- QL_SOCKET_LOG("devinfo:");
- hc_flash_data_printf((unsigned char *)devinfo, 64);
- uint8_t sim_id = 0;
- char siminfo[64] = {0};
- QL_SOCKET_LOG("siminfo:");
- hc_flash_data_printf((unsigned char *)siminfo, 64);
- ret = ql_sim_get_iccid(sim_id, siminfo, 64);
- npt_func_get_info(nSim, profile_idx);
- mt();
- while(1)
- {
-
- fd_changed = select(socket_fd+1, &read_fds, &write_fds, &exp_fds, NULL);
- if(fd_changed > 0)
- {
- if(FD_ISSET(socket_fd, &write_fds))
- {
- FD_CLR(socket_fd, &write_fds);
- if(connected== 0)
- {
- int value = 0;
- int len = 0;
-
- len = sizeof(value);
- getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, &value, &len);
-
- QL_SOCKET_LOG("errno: %d", value);
- if(value == 0 || value == EISCONN )
- {
- QL_SOCKET_LOG("=====connect to \"220.180.239.212:8252\" success=====");
- connected = 1;
- FD_SET(socket_fd, &read_fds);
- memset(send_buf, 0x00, 128);
- send_len = snprintf(send_buf, 128,"%d%s%d\r\n",i,"startAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAend",i);
- write(socket_fd, send_buf, send_len);
- i++;
- }else
- {
- QL_SOCKET_LOG("=====connect to \"220.180.239.212:8252\" failed=====");
- close(socket_fd);
- socket_fd = -1;
- goto reboot;
- }
- }else
- {
- memset(send_buf, 0x00, 128);
- send_len = snprintf(send_buf, 128,"%d%s%d\r\n",i,"startAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAend",i);
- write(socket_fd, send_buf, send_len);
- i++;
- }
- }else if(FD_ISSET(socket_fd, &read_fds))
- {
- FD_CLR(socket_fd, &read_fds);
- memset(recv_buf,0x00, 128);
- recv_len = read(socket_fd, recv_buf, 128);
- if(recv_len > 0)
- {
- for(int i = 0;i < recv_len;i++)
- QL_SOCKET_LOG(">>>>Recv: %x \n", recv_buf[i]);
- QL_SOCKET_LOG("chengwei recv ok\n");
- memset(send_buf, 0x00, 128);
- send_len = snprintf(send_buf, 128,"%d%s%d\r\n",i,"startAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAend",i);
- write(socket_fd, send_buf, send_len);
- i++;
- FD_SET(socket_fd, &read_fds);
- if(ql_nw_get_data_count(nSim, &data_count_info) == 0)
- {
- QL_SOCKET_LOG("data count: uplink %llu, downlink %llu", data_count_info.uplink_data_count, data_count_info.downlink_data_count);
- }
- }else if(recv_len == 0)
- {
- if(closing == false)
- {
- QL_SOCKET_LOG("===passive close!!!!");
- shutdown(socket_fd, SHUT_WR);
- closing = true;
- FD_SET(socket_fd, &read_fds);
- }else
- {
- close(socket_fd);
- socket_fd = -1;
- goto reboot;
- }
- }else
- {
- if(lwip_get_error(socket_fd) == EAGAIN)
- {
- FD_SET(socket_fd, &read_fds);
- }else
- {
- close(socket_fd);
- socket_fd = -1;
- goto reboot;
- }
- }
- }else if(FD_ISSET(socket_fd, &exp_fds))
- {
- FD_CLR(socket_fd, &exp_fds);
- close(socket_fd);
- socket_fd = -1;
- goto reboot;
- }
-
- }
- reboot:
- if(socket_fd < 0)
- {
- }
- }
- if(pres!=NULL)
- {
- freeaddrinfo(pres);
- pres = NULL;
- }
- goto loop;
- exit:
- ql_rtos_task_delete(socket_demo_task);
-
- return;
- }
- #endif
- int ql_socket_app_init(void)
- {
- QlOSStatus err = QL_OSI_SUCCESS;
-
- err = ql_rtos_task_create(&socket_demo_task, 16*1024, APP_PRIORITY_ABOVE_NORMAL, "QsocketApp", socket_app_thread, NULL, 5);
- if(err != QL_OSI_SUCCESS)
- {
- QL_SOCKET_LOG("socket_app init failed");
- }
-
- return err;
- }
- /*
- static UTC8TimeType UTC8TimeTcp;
- static void vTimer1000msCallback(TimerHandle_t pxTimer)
- {
- uint32 ulTimerID;
- ulTimerID = (uint32)pvTimerGetTimerID(pxTimer);
- sint16 *TcpDataFeqPtr =NULL;
- TcpDataFeqPtr = (sint16 *)&TcpDataFeq;
- if (ulTimerID == 0)
- {
- if(SocketId>=0)
- {
- for(uint8 i=2;i<sizeof(TcpDataFeq)/sizeof(sint16);i=i+3)
- {
- *(TcpDataFeqPtr+i) = (*(TcpDataFeqPtr+i)-1)<0 ? 0 : (*(TcpDataFeqPtr+i)-1);
- }
- }
- TimeUpdate(&UTC8TimeTcp);
- }
- }
- */
- //osiTimer_t *osiTimerCreate(osiThread_t *thread, osiCallback_t cb, void *ctx);
- //bool osiTimerSetPeriod(osiTimer_t *timer, uint32_t ms, bool periodic);
- //bool osiTimerSetPeriodRelaxed(osiTimer_t *timer, uint32_t ms, uint32_t relaxed_ms, bool periodic);
-
- static uint32_t run_num = 0;
- static osiTimer_t *timer1=NULL;
- static void timer1_callback_function(void)
- {
- run_num++;
- QL_SOCKET_LOG("creat my timer ok %d\n", run_num);
- osiTimerStartRelaxed(timer1, 1000, OSI_WAIT_FOREVER);
- }
-
- static void mt(void)
- {
- //osiThread_t *thread = osiThreadCurrent();
- timer1 = osiTimerCreate(NULL,(osiCallback_t)timer1_callback_function, NULL);
- if(timer1!=NULL)
- QL_SOCKET_LOG("creat my timer ok\n");
- else
- QL_SOCKET_LOG("creat my timer err\n");
- bool ret = osiTimerStart(timer1, 0);
- if(ret ==true)
- QL_SOCKET_LOG("start my timer ok\n");
- else
- QL_SOCKET_LOG("start my timer err\n");
- }/**/
|