/*================================================================================
Module Name: test_api_main.c
General Description: Test tcmd api lib.
==================================================================================
borqs Confidential Proprietary
Template ID and version: TMP_LFC_50085 Version 1.1
(c) Copyright Motorola 2004, All Rights Reserved
Revision History:
Modification Tracking
Author (core ID) Date Number Description of Changes
------------------------ ------------ ---------- -------------------------
Yadong Zhu 06/12/2008 Created
Portability: Indicate if this module is portable to other compilers or
platforms. If not, indicate specific reasons why is it not portable.
==================================================================================
INCLUDE FILES
================================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "TC_Net.h"
#include <time.h>
/*================================================================================
LOCAL CONSTANTS
================================================================================*/
/*================================================================================
LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
================================================================================*/
/*================================================================================
LOCAL MACROS
================================================================================*/
/*================================================================================
LOCAL FUNCTION PROTOTYPES
================================================================================*/
/*================================================================================
LOCAL VARIABLES
================================================================================*/
/*================================================================================
GLOBAL VARIABLES
================================================================================*/
/*================================================================================
LOCAL POINTER DECLARATIONS
================================================================================*/
/*================================================================================
LOCAL FUNCTIONS
================================================================================*/
unsigned char *hexstr_to_hex(int length, const char *hexstr);
/*================================================================================
GLOBAL FUNCTIONS
================================================================================*/
#define counter_loop 3
int main(int argc, char **argv)
{
int ret = -1;
int socket = -1;
unsigned char res_code;
unsigned int res_length;
unsigned char res_data_buff[300];
unsigned short op_code = 0;
unsigned int length = 0;
unsigned char *data_buff = NULL;
unsigned int bytes_length;
int seconds=5;
time_t old;
time_t new;
memset(res_data_buff,0,300);
//added bu lizhenqing,so can connect once and send twice
int loop_counter=0;
switch(argc)
{
case 1:
printf("%s use <>:\n", argv[0]);
printf("%s opcode [length command_string]\n", argv[0]);
exit(0);
break;
case 2:
op_code = atoi(argv[1]);
break;
case 3:
printf("%s opcode length command_string\n", argv[0]);
exit(0);
break;
case 4:
printf("argv[1] %s\n", argv[1]);
op_code = atoi(argv[1]);
if(op_code >= 0xE00)
{
printf("no this command\n");
exit(0);
}
length = atoi(argv[2]);
printf("argv[2] %s\n", argv[2]);
if(length == 0)
{
printf("please input command length\n");
exit(0);
}
printf("argv[3] %s\n", argv[3]);
data_buff = hexstr_to_hex(length, argv[3]);
bytes_length = (length % 2) + (length / 2);
break;
}
ret = tc_client_socket(DEFAULTPORT1);
if(ret == -1)
{
printf("start tcmd client failured\n");
free(data_buff);
exit(1);
}
socket = ret;
while( loop_counter++ < counter_loop ){
printf("------------------send loop %d\n",loop_counter);
switch(argc)
{
case 2:
if(loop_counter==1)
{
ret = tc_api_send_socket(socket, op_code, 0, NULL);
}
else if(loop_counter==2)
{
ret = tc_api_send_socket(socket, 41, 0, NULL);//empty handler
}
else if(loop_counter==3)
{
ret = tc_api_send_socket(socket, 48, 0, NULL);//baterray
}
else
{
}
break;
case 4:
ret = tc_api_send_socket(socket, op_code, bytes_length, data_buff);
break;
}
if(ret == -1)
{
printf("send tcmd command failured\n");
free(data_buff);
exit(1);
}
/*
ret= tc_recv_socket(socket,(void*)res_data_buff,300);
//ret = TCMD_API_Recv_Command(socket, &res_code, &res_length, (void*)&res_data_buff);
if(ret == -1)
{
printf("receive tcmd command response failured\n");
free(data_buff);
exit(1);
}
printf("=======receive response length %d\n", ret);
res_data_buff[ret]='\0';
if(ret > 0)
{
int i = 0;
printf("==============receive response data buff\n");
printf("==============receive response data buff%s:\n" , res_data_buff);
for(i = 0; i < ret; i++)
{
printf("%x", res_data_buff[i]);
}
printf("\n");
}
*/
//printf("sleep 2 seconds\n");
/*sleep(1);
time(&old);
time(&new);
while(old+seconds>new){
printf("sleep 5 seconds\n");
time(&new);
}
*/
/*
sleep(2);
printf("sleep 3 seconds\n");
sleep(1);
sleep(1);
sleep(1);
printf("sleep 3seconds\n");
sleep(1);
sleep(1);
sleep(1);
*/
}
printf("sleep 20 seconds before close socket\n");
sleep(20);
free(data_buff);
printf("=close socket\n");
close(socket);
exit(0);
}
unsigned char *hexstr_to_hex(int length, const char *hexstr)
{
int j, k;
unsigned char* buff;
unsigned char* new_buff;
int bytes_length;
k = 0;
bytes_length = (length % 2) + (length / 2);
buff =(unsigned char*)malloc(length);
printf("hex str %s\n", hexstr);
memcpy(buff, hexstr, length);
printf("buff str %s\n", buff);
new_buff = malloc(bytes_length);
memset(new_buff, 0, bytes_length);
for(j = length - 1; j >= 0; j--)
{
if((k % 2) == 0)
{
if(buff[j] >= '0' && buff[j] <= '9')
{
new_buff[j/2] = ((buff[j] - 48) & 0x0F);
} else if(buff[j] >= 'a' && buff[j] <= 'f')
{
new_buff[j/2] = ((buff[j] - 97 + 10) & 0x0F);
} else if(buff[j] >= 'A' && buff[j] <= 'F')
{
new_buff[j/2] = ((buff[j] - 65 + 10) & 0x0F);
} else
{
printf("must be hex decime\n");
free(buff);
free(new_buff);
return NULL;
}
if( j > 0)
{
if(buff[j - 1] >= '0' && buff[j - 1] <= '9')
{
new_buff[j/2] |= (((buff[j-1] - 48) << 4) & 0xF0);
} else if(buff[j - 1] >= 'a' && buff[j - 1] <= 'f')
{
new_buff[j/2] |= (((buff[j-1] - 97 + 10) << 4) & 0xF0);
} else if(buff[j - 1] >= 'A' && buff[j - 1] <= 'F')
{
new_buff[j/2] |= (((buff[j-1] - 65 + 10) << 4) & 0xF0);
} else
{
printf("must be hex decime\n");
free(buff);
free(new_buff);
return NULL;
}
}
}
if((k % 2) == 1)
{
k++;
continue;
}
k++;
}
printf("%s\n", buff);
for(k = 0; k < bytes_length; k++)
{
printf("%x\n", new_buff[k]);
}
return new_buff;
}
|