HRESULT parse_conn_param_serial(const char *opt, struct CONN_PARAM_COM *param) { int type, n = 0; long tmp; char *top, *pos, *pos_param[_PARA_MAX_COM]; char *opt_cpy = NULL; HRESULT hr = E_INVALIDARG; if(param != NULL){ type = parse_conn_type(opt); if (type == CONN_COM) { /* Copy connection option string */ opt_cpy = (char *)malloc(strlen(opt) + 1); if(opt_cpy == NULL){ hr = E_OUTOFMEMORY; goto exit_proc; } strcpy(opt_cpy, opt); /* Split connection option string */ top = opt_cpy; while(1){ if(n >= _PARA_MAX_COM){ goto exit_proc; } pos_param[n++] = top; pos = strchr(top, ':'); if (pos == NULL) { break; } *pos = '\0'; top = (pos + 1); } if(n == 4 || n == 5){ goto exit_proc; } /* Flow */ if(n >= 7){ if(is_numeric(pos_param[6]) == 0){ goto exit_proc; } tmp = atol(pos_param[6]); if(tmp < 0 || 3 < tmp){ hr = DISP_E_OVERFLOW; goto exit_proc; } param->flow = (char)tmp; } /* Stop bits */ if (n >= 6) { tmp = (int)(atof(pos_param[5]) * 10.0); switch(tmp){ case 10: param->stop_bits = ONESTOPBIT; break; case 20: param->stop_bits = TWOSTOPBITS; break; default: goto exit_proc; } } /* Data bits */ if (n >= 5) { if(is_numeric(pos_param[4]) == 0){ goto exit_proc; } tmp = atol(pos_param[4]); if(tmp < 5 || 8 < tmp){ hr = DISP_E_OVERFLOW; goto exit_proc; } param->data_bits = (char)tmp; } /* Parity */ if (n >= 4) { if (strlen(pos_param[3]) != 1) { goto exit_proc; } switch (*pos_param[3]) { case L'N': case L'n': param->parity = NOPARITY; break; case L'O': case L'o': param->parity = ODDPARITY; break; case L'E': case L'e': param->parity = EVENPARITY; break; default: goto exit_proc; } } /* Baud rate */ if (n >= 3) { if(is_numeric(pos_param[2]) == 0){ goto exit_proc; } param->baud_rate = (uint32_t)atol(pos_param[2]); } /* Port */ if(n >= 2){ if(is_numeric(pos_param[1]) == 0){ goto exit_proc; } tmp = atol(pos_param[1]); if((tmp < 0) || (tmp != (int)tmp)){ hr = DISP_E_OVERFLOW; goto exit_proc; } param->port = tmp; } hr = S_OK; } } exit_proc: if(opt_cpy != NULL){ free(opt_cpy); opt_cpy = NULL; } return hr; }
HRESULT bCap_Open_Server(const char *connect, uint32_t timeout, int *pfd) { int type, index, *sock; HRESULT hr; void *conn_param; struct CONN_PARAM_ETH eth_param = { 0, 0, htonl(INADDR_ANY), htons(5007) }; struct CONN_PARAM_COM com_param = { 1, 38400, NOPARITY, 8, ONESTOPBIT, 0 }; struct CONN_BCAP_SERVER *bcap_param; struct CONN_PARAM_COMMON *device; struct sockaddr_in *paddr; if(connect == NULL || pfd == NULL) return E_INVALIDARG; type = parse_conn_type(connect); index = find_open_address(type); if(index == 0) return E_MAX_OBJECT; bcap_param = &m_conn_param[index - 1]; device = &bcap_param->device; /* Initializes connection parameters */ device->type = type; switch(device->type) { case CONN_TCP: hr = parse_conn_param_ether(connect, ð_param); conn_param = ð_param; device->arg = NULL; device->dn_open = &tcp_open_server; device->dn_close = &tcp_close; device->dn_send = &tcp_send; device->dn_recv = &tcp_recv; device->dn_set_timeout = &tcp_set_timeout; break; case CONN_UDP: hr = parse_conn_param_ether(connect, ð_param); conn_param = ð_param; paddr = (struct sockaddr_in *) malloc(sizeof(struct sockaddr_in)); if(paddr == NULL) { hr = E_OUTOFMEMORY; break; } paddr->sin_addr.s_addr = eth_param.dst_addr; paddr->sin_port = eth_param.dst_port; paddr->sin_family = AF_INET; device->arg = (void *) paddr; device->dn_open = &udp_open; device->dn_close = &udp_close; device->dn_send = &udp_send; device->dn_recv = &udp_recv; device->dn_set_timeout = &udp_set_timeout; break; case CONN_COM: hr = parse_conn_param_serial(connect, &com_param); conn_param = &com_param; device->arg = NULL; device->dn_open = &com_open; device->dn_close = &com_close; device->dn_send = &com_send; device->dn_recv = &com_recv; device->dn_set_timeout = &com_set_timeout; break; default: hr = E_INVALIDARG; break; } if(FAILED(hr)) { if(device->arg != NULL) { free(device->arg); device->arg = NULL; } memset(bcap_param, 0, sizeof(struct CONN_BCAP_SERVER)); return hr; } /* Create terminal event for main thread */ hr = create_event(&bcap_param->term_main_evt, 1, 0); if(FAILED(hr)) { if(device->arg != NULL) { free(device->arg); device->arg = NULL; } memset(bcap_param, 0, sizeof(struct CONN_BCAP_SERVER)); return hr; } /* Opens connection */ sock = &device->sock; hr = device->dn_open(conn_param, sock); if(FAILED(hr)) { destroy_event(&bcap_param->term_main_evt); if(device->arg != NULL) { free(device->arg); device->arg = NULL; } memset(bcap_param, 0, sizeof(struct CONN_BCAP_SERVER)); return hr; } hr = device->dn_set_timeout(*sock, timeout); if(FAILED(hr)) { bCap_Close_Server(&index); return hr; } /* Sets parameters */ device->timeout = timeout; bcap_param->exec_timeout = INIT_EXEC_TIMEOUT; bcap_param->wdt_interval = INIT_WDT_INTERVAL; /* Begins main thread */ if(device->type == CONN_TCP) { begin_thread(&bcap_param->main_thread, &accept_thread, bcap_param); } else { begin_thread(&bcap_param->main_thread, &recv_thread, bcap_param); } *pfd = index; return S_OK; }
HRESULT parse_conn_param_ether(const char *opt, struct CONN_PARAM_ETH *param) { int tmp, type, n = 0; uint32_t uitmp; char *top, *pos, *pos_param[_PARA_MAX_ETH]; char *opt_cpy = NULL; HRESULT hr = E_INVALIDARG; if(param != NULL){ type = parse_conn_type(opt); if ((type == CONN_TCP) || (type == CONN_UDP)) { /* Copy connection option string */ opt_cpy = (char *)malloc(strlen(opt) + 1); if(opt_cpy == NULL){ hr = E_OUTOFMEMORY; goto exit_proc; } strcpy(opt_cpy, opt); /* Split connection option string */ top = opt_cpy; while(1){ if(n >= _PARA_MAX_ETH){ goto exit_proc; } pos_param[n++] = top; pos = strchr(top, ':'); if (pos == NULL) { break; } *pos = '\0'; top = (pos + 1); } /* Source Port */ if(n >= 5) { if (is_numeric(pos_param[4]) == 0){ goto exit_proc; } tmp = atoi(pos_param[4]); if((tmp < 0) || (tmp != (uint16_t)tmp)){ hr = DISP_E_OVERFLOW; goto exit_proc; } param->src_port = htons(tmp); } /* Source IP */ if (n >= 4) { uitmp = inet_addr(pos_param[3]); if(uitmp == htonl(INADDR_NONE) && strcmp(pos_param[3], "255.255.255.255") != 0){ goto exit_proc; } param->src_addr = uitmp; } /* Dest Port */ if(n >= 3) { if (is_numeric(pos_param[2]) == 0){ goto exit_proc; } tmp = atoi(pos_param[2]); if((tmp < 0) || (tmp != (uint16_t)tmp)){ hr = DISP_E_OVERFLOW; goto exit_proc; } param->dst_port = htons(tmp); } /* Dest IP */ if (n >= 2) { uitmp = inet_addr(pos_param[1]); if(uitmp == htonl(INADDR_NONE) && strcmp(pos_param[1], "255.255.255.255") != 0){ goto exit_proc; } param->dst_addr = uitmp; } hr = S_OK; } } exit_proc: if(opt_cpy != NULL){ free(opt_cpy); opt_cpy = NULL; } return hr; }