ll | d164b02 | 2022-03-29 14:01:01 +0800 | [diff] [blame^] | 1 | #include<sys/types.h> |
| 2 | #include<sys/socket.h> |
| 3 | #include<unistd.h> |
| 4 | #include<arpa/inet.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <signal.h> |
| 8 | #include <string.h> |
| 9 | #include <log/log.h> |
| 10 | #include "liblog/lynq_deflog.h" |
| 11 | #include "include/lib_at/lynq_at.h" |
| 12 | |
| 13 | #define LYNQ_AT_SERVICE_PORT 8087 |
| 14 | #define OUT_MAX_SIZE 1024 |
| 15 | #define USER_LOG_TAG "LYNQ_AT" |
| 16 | |
| 17 | int sockfd = 0; |
| 18 | char *output = NULL; |
| 19 | struct sockaddr_in addr_serv; |
| 20 | socklen_t len; |
| 21 | |
| 22 | /** |
| 23 | * @brief type:in send third at cmd to service |
| 24 | * @param ext_at type:in input at cmd |
| 25 | * @param callback type:in |
| 26 | * @return int |
| 27 | */ |
| 28 | int lynq_reg_third_at(const char *ext_at, LYNQ_AT_CALLBACK callback) |
| 29 | { |
| 30 | LYDBGLOG("lynq_reg_third_at start\n"); |
| 31 | sockfd=socket(AF_INET,SOCK_DGRAM,0); |
| 32 | memset(&addr_serv, 0, sizeof(addr_serv)); |
| 33 | addr_serv.sin_family =AF_INET; |
| 34 | addr_serv.sin_port =htons(LYNQ_AT_SERVICE_PORT); |
| 35 | addr_serv.sin_addr.s_addr = htonl(INADDR_ANY); |
| 36 | len=sizeof(addr_serv); |
| 37 | int len_buf = strlen(ext_at); |
| 38 | int send = sendto(sockfd, ext_at, len_buf,0,(struct sockaddr*)&addr_serv,len); |
| 39 | if(send < 0) |
| 40 | { |
| 41 | LYDBGLOG("send fail\n"); |
| 42 | return -1; |
| 43 | } |
| 44 | char *input = NULL; |
| 45 | output = (char *)malloc(sizeof(char)*OUT_MAX_SIZE); |
| 46 | if(NULL == output) |
| 47 | { |
| 48 | LYDBGLOG("malloc fail\n"); |
| 49 | return -1; |
| 50 | } |
| 51 | while (1) |
| 52 | { |
| 53 | /*receive at cmd*/ |
| 54 | LYDBGLOG("lynq_reg_third_at receive at cmd\n"); |
| 55 | char at_cmd[100] = {0}; |
| 56 | int recv = recvfrom(sockfd,at_cmd,sizeof(at_cmd),0,(struct sockaddr*)&addr_serv,&len); |
| 57 | if(recv < 0) |
| 58 | { |
| 59 | LYDBGLOG("recv fail\n"); |
| 60 | return -1; |
| 61 | } |
| 62 | input = at_cmd; |
| 63 | callback(input, output, OUT_MAX_SIZE); |
| 64 | if(NULL == output) |
| 65 | { |
| 66 | LYDBGLOG("output = null\n"); |
| 67 | return -1; |
| 68 | } |
| 69 | LYDBGLOG("lynq_reg_third_at send output to service\n"); |
| 70 | int send = sendto(sockfd, output, strlen(output),0,(struct sockaddr*)&addr_serv,len); |
| 71 | if(send < 0) |
| 72 | { |
| 73 | LYDBGLOG("send fail\n"); |
| 74 | continue; |
| 75 | } |
| 76 | } |
| 77 | if(output != NULL) |
| 78 | { |
| 79 | free(output); |
| 80 | output = NULL; |
| 81 | } |
| 82 | if(sockfd != 0) |
| 83 | { |
| 84 | close(sockfd); |
| 85 | } |
| 86 | return 0; |
| 87 | } |
| 88 | |