lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /*******************************************************************************
|
| 2 | * Ö÷Òª¹¦ÄÜÊÇÉèÖÃCMUX¹¦ÄÜÇл»
|
| 3 | * Ö§³ÖµÄ¹¦ÄÜÈçÏÂ:
|
| 4 | * ²éѯ»òÉèÖÃCMUXģʽÇл»
|
| 5 | *
|
| 6 | * ºóÐøÈçÓÐCMUX¹¦ÄÜÏà¹ØµÄ״̬²éѯ»òÉèÖõÄÏà¹ØÐèÇ󣬿ÉÒÔÔÚ´ËÎļþÖÐÌí¼ÓATÃüÁî´¦Àí
|
| 7 | **********************************************************************************/
|
| 8 |
|
| 9 |
|
| 10 | #if (APP_OS_TYPE == APP_OS_LINUX)
|
| 11 | #include "ext_cmux_func.h"
|
| 12 | #include "ext_dev_func.h"
|
| 13 |
|
| 14 |
|
| 15 | /**************************************************************************
|
| 16 | * È«¾Ö±äÁ¿¶¨ÒåÇø
|
| 17 | **************************************************************************/
|
| 18 |
|
| 19 | int32_t g_cmux_flag = 0;//Åжϵ±Ç°ÊÇ·ñÊÇcmux״̬
|
| 20 | int32_t g_commonPortinCmux = -1; //Çл»³Écmux״̬ʱÆÕͨģʽÏÂÎļþÃèÊö·û
|
| 21 | int32_t g_selfAdaptFlag = 0; //±ê¼ÇÊÇ·ñÐèÒªÅжϲ¨ÌØÂÊ×ÔÊÊÓ¦£¬0:²»ÐèÒª£¬1:ÐèÒª
|
| 22 | int32_t g_iUartPort = -1; //µ±Ç°UartÉ豸¾ä±ú
|
| 23 |
|
| 24 | static pthread_t s_uiMonitorHungupThread = -1; //´¦Àí¼à¿ØhungupµÄÏß³Ì
|
| 25 |
|
| 26 | #define N_GSM0710 21 //cmuxÏà¹ØÅäÖÃ
|
| 27 | #define cmuxChID 1 //µ±Ç°Ê¹ÓõÄcmuxͨµÀºÅ
|
| 28 |
|
| 29 |
|
| 30 | //========================================================================================================//
|
| 31 | //º¯ÊýʵÏÖÇø
|
| 32 | //========================================================================================================//
|
| 33 |
|
| 34 | static int zUP_SetPort(int32_t iFd)
|
| 35 | {
|
| 36 | if(iFd < 0)
|
| 37 | {
|
| 38 | at_print(AT_ERR,"Set usb port error. iFd == %d\n", iFd);
|
| 39 | return 0;
|
| 40 | }
|
| 41 | at_print(AT_ERR,"Set usb port ok. iFd == %d\n", iFd);
|
| 42 | //ioctl(iFd, (('R'<<8)|1|(0x4004<<16)), 0x400);
|
| 43 | //ioctl(iFd, (('R'<<8)|4|(0x4004<<16)), 0);
|
| 44 | vFnSetUsbInfo(iFd, &s_tUsbInfo, AUTO_MODE);
|
| 45 | return 1;
|
| 46 | }
|
| 47 |
|
| 48 | static void zUP_MuxChannelConfig(int32_t iFd)
|
| 49 | {
|
| 50 | static struct termios Muxtio;
|
| 51 |
|
| 52 | tcgetattr(iFd, &Muxtio); // save current port settings
|
| 53 | Muxtio.c_cflag = CS8 | CLOCAL | CREAD;
|
| 54 |
|
| 55 |
|
| 56 | Muxtio.c_iflag = IGNPAR;
|
| 57 | Muxtio.c_oflag = 0;
|
| 58 | Muxtio.c_lflag = 0; //!ICANON;
|
| 59 |
|
| 60 | Muxtio.c_cc[VMIN] = 1;
|
| 61 | Muxtio.c_cc[VTIME] = 0;
|
| 62 | //cfsetospeed(&Muxtio, B38400);
|
| 63 | //cfsetispeed(&Muxtio, B38400);
|
| 64 | tcsetattr(iFd, TCSANOW, &Muxtio);
|
| 65 | }
|
| 66 |
|
| 67 | int zUP_CreateCMUXATThread()
|
| 68 | {
|
| 69 | char shellCmd[AT_CMD_MAX] = {0};
|
| 70 | char mainDevNum[AT_CMD_MAX] = {0};
|
| 71 | char cmuxDevice[AT_CMD_MAX] = {0};
|
| 72 | FILE *fp = NULL;
|
| 73 | int32_t result = -1;
|
| 74 | int32_t g_modeToCMUXAT = -1; //CMUX1µÄÎļþÃèÊö·û
|
| 75 |
|
| 76 | system("rm -f /tmp/uartproxy_file");
|
| 77 | system("cat /proc/devices | grep gsmtty > /tmp/uartproxy_file");
|
| 78 | fp = fopen("/tmp/uartproxy_file", "r");
|
| 79 | if(NULL == fp)
|
| 80 | {
|
| 81 | at_print(AT_ERR,"can not open file /tmp/uartproxy_file\n");
|
| 82 | return -1;
|
| 83 | }
|
| 84 | memset(shellCmd, 0, sizeof(shellCmd));
|
| 85 | fgets(shellCmd, sizeof(shellCmd), fp);
|
| 86 | at_print(AT_ERR,"shellCmd= %s!!!\n", shellCmd);
|
| 87 | memcpy(mainDevNum, shellCmd, (int32_t)(strchr(shellCmd,' ') - shellCmd));
|
| 88 | at_print(AT_ERR,"mainDevNum= %s!!!\n", mainDevNum);
|
| 89 | fclose(fp);
|
| 90 | memset(shellCmd, 0, sizeof(shellCmd));
|
| 91 | snprintf(shellCmd, sizeof(shellCmd), "/bin/mknod /dev/gsmtty0 c %s 0", mainDevNum);//cmuxͨµÀ0±ØÐë´´½¨
|
| 92 | result = zxic_system(shellCmd);
|
| 93 | at_print(AT_ERR,"result1= %d!!!\n", result);
|
| 94 | memset(shellCmd, 0, sizeof(shellCmd));
|
| 95 | snprintf(shellCmd, sizeof(shellCmd),"/bin/mknod /dev/gsmtty%d c %s %d", cmuxChID, mainDevNum, cmuxChID);
|
| 96 | result = zxic_system(shellCmd);
|
| 97 | at_print(AT_ERR,"result2= %d!!!\n", result);
|
| 98 | sprintf(cmuxDevice, "/dev/gsmtty%d", cmuxChID);//»ñÈ¡cmuxÉ豸Ãû
|
| 99 | g_modeToCMUXAT = open(cmuxDevice, O_RDWR);
|
| 100 | if(g_modeToCMUXAT < 0)
|
| 101 | {
|
| 102 | // UART_PROXY_LOG("Open port %d error.\n", g_modeToCMUXAT);
|
| 103 | return 0;
|
| 104 | }
|
| 105 | g_commonPortinCmux = g_iUartPort;
|
| 106 | g_iUartPort = g_modeToCMUXAT;
|
| 107 | at_print(AT_ERR,"use gsmtty%d start!!!!\n", cmuxChID);
|
| 108 |
|
| 109 | zUP_MuxChannelConfig(g_iUartPort);
|
| 110 |
|
| 111 | return 1;
|
| 112 | }
|
| 113 |
|
| 114 | static int32_t zUP_GetUartEvents(int32_t fd)
|
| 115 | {
|
| 116 | struct pollfd fds;
|
| 117 | int32_t time_delay = 300;
|
| 118 | fds.fd = fd;
|
| 119 | fds.events = POLLHUP;
|
| 120 | if(poll(&fds, 1, time_delay) <= 0)
|
| 121 | {
|
| 122 | return 0;
|
| 123 | }
|
| 124 | if(fds.revents)
|
| 125 | {
|
| 126 | return 1;
|
| 127 | }
|
| 128 | return 0;
|
| 129 | }
|
| 130 |
|
| 131 | static void *zUP_MonitorHungupHander()
|
| 132 | {
|
| 133 | int32_t result = 0;
|
| 134 | while(1)
|
| 135 | {
|
| 136 | result = zUP_GetUartEvents(g_commonPortinCmux);
|
| 137 | at_print(AT_DEBUG,"tty GetUartEvents ret is %d\n",result);
|
| 138 | if(result > 0)
|
| 139 | {
|
| 140 | at_print(AT_DEBUG,"GetUartEvents ret is %d\n",result);
|
| 141 |
|
| 142 | //·¢ËÍÏûÏ¢¸øat_Ctl£¬½øÐÐmux2USBµÄ²Ù×÷£¬ÖØÐ»ñÈ¡µ½fd
|
| 143 | //ipc_send_message(MODULE_ID_AT_CTL,MODULE_ID_AT_CTL, XXX, 0, NULL,0);
|
| 144 | #if 0
|
| 145 | if(zUP_UartOpenConfig() == 0)
|
| 146 | {
|
| 147 | at_print(AT_DEBUG,"zUP_UartOpenConfig 0 %d\n");
|
| 148 | return NULL;
|
| 149 | }
|
| 150 | g_cmux_flag = 0;
|
| 151 | zUP_Mux2USB(g_iUartPort);
|
| 152 | #endif
|
| 153 |
|
| 154 | g_cmux_flag = 0;
|
| 155 | pthread_cancel(s_uiMonitorHungupThread);
|
| 156 | }
|
| 157 | }
|
| 158 | return NULL;
|
| 159 | }
|
| 160 |
|
| 161 |
|
| 162 | int zUP_CreateMonitorHungupThread()
|
| 163 | {
|
| 164 | int32_t iResult = -1;
|
| 165 | pthread_attr_t attr;
|
| 166 |
|
| 167 | pthread_attr_init(&attr);
|
| 168 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
| 169 | iResult = pthread_create(&s_uiMonitorHungupThread, &attr, zUP_MonitorHungupHander, NULL);
|
| 170 | pthread_attr_destroy(&attr);
|
| 171 | if(0 != iResult)
|
| 172 | {
|
| 173 | at_print(AT_ERR,"Call MonitorHungup pthread_create() error.\n");
|
| 174 | return 0;
|
| 175 | }
|
| 176 | return 1;
|
| 177 | }
|
| 178 |
|
| 179 | void OpenMultiCmuxChannel()
|
| 180 | {
|
| 181 | // UART_PROXY_LOG("Enter %s, and will go open multi mux_fd\n", __FUNCTION__);
|
| 182 |
|
| 183 | if(!zUP_CreateCMUXATThread())
|
| 184 | {
|
| 185 | at_print(AT_ERR,"Call zUP_CreateCMUXATThread() error.\n");
|
| 186 | return;
|
| 187 | }
|
| 188 | if(!zUP_CreateMonitorHungupThread())
|
| 189 | {
|
| 190 | at_print(AT_ERR,"Call zUP_CreateMonitorHungupThread() error.\n");
|
| 191 | return;
|
| 192 | }
|
| 193 |
|
| 194 | }
|
| 195 |
|
| 196 | static void bFnCmuxProc()
|
| 197 | {
|
| 198 | int ldisc = N_GSM0710;
|
| 199 | int ret = 0;
|
| 200 | struct gsm_config cmux_config;
|
| 201 |
|
| 202 | g_cmux_flag = 1;
|
| 203 | ret = ioctl(g_iUartPort, TIOCSETD, &ldisc);//Çл»Ïß·¹æ³Ì
|
| 204 |
|
| 205 | at_print(AT_DEBUG,"tty goto mux ret is %d\n",ret);
|
| 206 | ret = ioctl(g_iUartPort, GSMIOC_GETCONF, &cmux_config);
|
| 207 | at_print(AT_DEBUG,"get mux ret is %d\n",ret);
|
| 208 |
|
| 209 | cmux_config.initiator = 0;
|
| 210 | cmux_config.encapsulation = 0;
|
| 211 | cmux_config.mru = 512;
|
| 212 | cmux_config.mtu = 512;
|
| 213 | ret = ioctl(g_iUartPort, GSMIOC_SETCONF, &cmux_config);
|
| 214 | at_print(AT_DEBUG,"set mux ret is %d\n",ret);
|
| 215 | OpenMultiCmuxChannel();
|
| 216 | }
|
| 217 |
|
| 218 | struct at_cmux_req
|
| 219 | {
|
| 220 | char name[32];
|
| 221 | int cmux_mode; //»ù±¾/¸ß¼¶Ä£Ê½
|
| 222 | int subset; //Ö¡¸ñʽÀàÐÍ
|
| 223 | int port_speed; //cmux²¨ÌØÂÊ
|
| 224 | int frame_len; //×î´óÖ¡³¤(N1)
|
| 225 | int ack_time; //½ÓÊÕÈ·ÈÏʱ¼ä(T1)
|
| 226 | int recon_count; //×î´óÖØÁ¬´ÎÊý(N2)
|
| 227 | int resp_time; //ÏìӦʱ¼ä(T2)
|
| 228 | int awak_time; //»½ÐÑʱ¼ä(T3)
|
| 229 | int win_size; //´°¿Ú´óС
|
| 230 | };
|
| 231 |
|
| 232 | int zcmuxSet_req(int at_fd,char * at_paras,void * *res_msg,int * res_msglen)
|
| 233 | {
|
| 234 |
|
| 235 | struct at_cmux_req *req = NULL;
|
| 236 | req = malloc(sizeof(struct at_cmux_req));
|
| 237 | if(req == NULL){softap_assert("");return AT_END;}
|
| 238 | memset(req,0,sizeof(struct at_cmux_req));
|
| 239 | void *p[9] = {&req->cmux_mode,&req->subset,&req->port_speed,&req->frame_len,&req->ack_time,&req->recon_count,&req->resp_time,&req->awak_time,&req->win_size};
|
| 240 |
|
| 241 | if(at_paras==NULL)
|
| 242 | softap_assert("zcmuxSet_req:at_paras is null");
|
| 243 |
|
| 244 | parse_param2("%d,%d,%d,%d,%d,%d,%d,%d,%d",at_paras,p);
|
| 245 | at_print(AT_DEBUG,"zcmuxSet_req: cmux_mode = %d, subset = %d, port_speed = %d, frame_len = %d, ack_time = %d, recon_count = %d, resp_time = %d, awak_time = %d, win_size = %d\n",
|
| 246 | req->cmux_mode, req->subset, req->port_speed, req->frame_len, req->ack_time, req->recon_count,
|
| 247 | req->resp_time, req->awak_time, req->win_size);
|
| 248 |
|
| 249 | if(req->cmux_mode == 0 && req->subset >= 0 && req->subset <= 1 && req->port_speed >= 0 && req->port_speed <= 5 &&
|
| 250 | (req->frame_len >= 8||req->frame_len == 0) && req->frame_len <= 32768 && req->ack_time >= 0 && req->ack_time <= 255 && req->recon_count >= 0 && req->recon_count <= 100
|
| 251 | && req->resp_time >= 0 && req->resp_time <= 255 && req->awak_time == 0 && req->win_size == 0)
|
| 252 | {
|
| 253 | at_print(AT_ERR, "Enter zcmuxSet_req\n");
|
| 254 |
|
| 255 | //req->fd = at_fd;
|
| 256 | char *name = at_portmng_get_name_by_fd(at_fd);
|
| 257 | if(name)
|
| 258 | memcpy(req->name, name, sizeof(req->name));
|
| 259 | *res_msg = req;
|
| 260 | *res_msglen = sizeof(struct at_cmux_req);
|
| 261 | return AT_CONTINUE;
|
| 262 | }
|
| 263 | free(req);
|
| 264 | *res_msg = at_err_build(ATERR_PARAM_INVALID);
|
| 265 | *res_msglen = strlen(*res_msg);
|
| 266 |
|
| 267 | return AT_END;
|
| 268 | }
|
| 269 | int zcmuxSet_rsp(void *rsp_msg, void**ret, int *retlen)
|
| 270 | {
|
| 271 | int rsp = *(int *)rsp_msg;
|
| 272 | if(rsp)
|
| 273 | {
|
| 274 | *ret = at_ok_build();
|
| 275 | }
|
| 276 | else
|
| 277 | {
|
| 278 | *ret = at_err_build(ATERR_PROC_FAILED);
|
| 279 | }
|
| 280 | *retlen = strlen(*ret);
|
| 281 | return AT_END;
|
| 282 | }
|
| 283 |
|
| 284 | #endif
|