| #include<sys/types.h> |
| #include<sys/socket.h> |
| #include<unistd.h> |
| #include<arpa/inet.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <signal.h> |
| #include <string.h> |
| #include <log/log.h> |
| #include <pthread.h> |
| #include "liblog/lynq_deflog.h" |
| #include "include/lib_at/lynq_at.h" |
| |
| #define LYNQ_AT_SERVICE_PORT 8087 |
| #define OUT_MAX_SIZE 1024 |
| #define USER_LOG_TAG "LYNQ_AT" |
| |
| typedef enum |
| { |
| A_SUCCESS = 0, |
| A_ERROR = -1 |
| }LYNQ_AT_E; |
| |
| /*lei add : maybe sento/revc error*/ |
| int result = A_SUCCESS; |
| /*lei add : maybe sento/revc error*/ |
| int sockfd = 0; |
| char *output = NULL; |
| char buffer_at[OUT_MAX_SIZE] = {0}; |
| struct sockaddr_in addr_serv; |
| socklen_t len; |
| LYNQ_AT_CALLBACK tmp = NULL; |
| |
| /** |
| * @brief Catch exceptions and free malloc's memory |
| * |
| * @param signum Type:[IN] signal |
| */ |
| static void signal_handler(int signum) |
| { |
| switch(signum) |
| { |
| case SIGABRT: |
| LYDBGLOG("recv SIGABRT\n"); |
| break; |
| case SIGBUS: |
| LYDBGLOG("recv SIGBUS\n"); |
| break; |
| case SIGFPE: |
| LYDBGLOG("recv SIGFPE\n"); |
| break; |
| case SIGILL: |
| LYDBGLOG("recv SIGILL\n"); |
| break; |
| case SIGSEGV: |
| LYDBGLOG("recv SIGSEGV\n"); |
| break; |
| default: |
| LYDBGLOG("recv unknown signal\n"); |
| break; |
| } |
| if(NULL != output) |
| { |
| free(output); |
| output = NULL; |
| } |
| } |
| |
| /** |
| * @brief send third cmd to service and receive input,then send output to service |
| * |
| * @param parm |
| * @return void* |
| */ |
| void *thread_recv(void *parm) |
| { |
| signal(SIGPIPE, signal_handler); |
| signal(SIGABRT, signal_handler); |
| signal(SIGBUS, signal_handler); |
| signal(SIGFPE, signal_handler); |
| signal(SIGILL, signal_handler); |
| signal(SIGSEGV, signal_handler); |
| int recv = 0; |
| int send = 0; |
| char at_cmd[100] = {0}; |
| sockfd=socket(AF_INET,SOCK_DGRAM,0); |
| memset(&addr_serv, 0, sizeof(addr_serv)); |
| addr_serv.sin_family =AF_INET; |
| addr_serv.sin_port =htons(LYNQ_AT_SERVICE_PORT); |
| addr_serv.sin_addr.s_addr = htonl(INADDR_ANY); |
| len=sizeof(addr_serv); |
| int len_buf = strlen(buffer_at); |
| send = sendto(sockfd, buffer_at, len_buf,0,(struct sockaddr*)&addr_serv,len); |
| if(send < 0) |
| { |
| LYDBGLOG("thread_recv send fail\n"); |
| result = send; |
| return NULL; |
| } |
| char *input = NULL; |
| output = (char *)malloc(sizeof(char)*OUT_MAX_SIZE); |
| if(NULL == output) |
| { |
| LYDBGLOG("thread_recv malloc fail\n"); |
| result = A_ERROR; |
| return NULL; |
| } |
| while (1) |
| { |
| /*receive at cmd*/ |
| LYDBGLOG("lynq_reg_third_at receive at cmd\n"); |
| recv = recvfrom(sockfd,at_cmd,sizeof(at_cmd),0,(struct sockaddr*)&addr_serv,&len); |
| if(recv < 0) |
| { |
| LYDBGLOG("thread_recv recv fail\n"); |
| continue; |
| } |
| input = at_cmd; |
| //begin deal with callback |
| tmp(input, output, OUT_MAX_SIZE); |
| LYDBGLOG("lynq_reg_third_at send output to service\n"); |
| send = sendto(sockfd, output, strlen(output),0,(struct sockaddr*)&addr_serv,len); |
| if(send < 0) |
| { |
| LYDBGLOG("thread_recv send fail\n"); |
| continue; |
| } |
| } |
| free(output); |
| output = NULL; |
| if(sockfd != 0) |
| { |
| close(sockfd); |
| } |
| return NULL; |
| } |
| |
| /** |
| * @brief Threads are created to communicate with the server |
| * |
| * @return int |
| */ |
| int lynq_connect_service_start(void) |
| { |
| pthread_t lynq_at_tid = -1; |
| int rt = pthread_create(&lynq_at_tid, NULL, thread_recv, NULL); |
| if(rt < 0) |
| { |
| LYERRLOG("urc loop failure!!!\n"); |
| return -1; |
| } |
| return 0; |
| } |
| |
| /** |
| * @brief Type:[IN] send third at cmd to service |
| * @param ext_at Type:[IN] input at cmd |
| * @param callback Type:[IN] |
| * @return int |
| */ |
| int lynq_reg_third_at(const char *ext_at, LYNQ_AT_CALLBACK callback) |
| { |
| if(NULL == ext_at || NULL == callback) |
| { |
| return A_ERROR; |
| } |
| memcpy(buffer_at, ext_at, strlen(ext_at)); |
| tmp = callback; |
| LYLOGSET(LOG_INFO); |
| LYLOGEINIT(USER_LOG_TAG); |
| LYDBGLOG("lynq_reg_third_at start\n"); |
| int ret = lynq_connect_service_start(); |
| if(ret != 0) |
| { |
| LYDBGLOG("lynq_connect_service_start start failed\n"); |
| return A_ERROR; |
| } |
| return result; |
| } |
| |