char *test_parse_del() { char *resp, buf[BUFSIZE]; parsed_text *parsed = malloc(sizeof(parsed_text)); prime_buf(buf, "delete"); resp = parse_del(buf, parsed); mu_assert("delete != key error", STR_EQ(resp, "CLIENT_ERROR: no key provided for delete\r\n")); prime_buf(buf, "delete foo"); resp = parse_del(buf, parsed); mu_assert("delete foo != NULL", ((resp == NULL) && STR_EQ("foo", parsed->key))); return 0; }
char *test_parse_get() { char *resp, buf[BUFSIZE]; parsed_text *parsed = malloc(sizeof(parsed_text)); prime_buf(buf, "get"); resp = parse_get(buf, parsed); mu_assert("get != keys provided error", STR_EQ(resp, "CLIENT_ERROR: no keys provided in get command\r\n")); prime_buf(buf, "get foo"); resp = parse_get(buf, parsed); mu_assert("get foo != NULL", ((resp == NULL) && STR_EQ("foo", parsed->keys[0]))); prime_buf(buf, "gets foo bar"); resp = parse_get(buf, parsed); mu_assert("get foo bar != NULL", ((resp == NULL) && STR_EQ("foo", parsed->keys[0]) && STR_EQ("bar", parsed->keys[1]))); return 0; }
char *test_parse_change() { char *resp, buf[BUFSIZE]; parsed_text *parsed = malloc(sizeof(parsed_text)); prime_buf(buf, "incr"); resp = parse_change(buf, parsed); mu_assert("incr != key error", STR_EQ(resp, "CLIENT_ERROR: no key received\r\n")); prime_buf(buf, "incr foo"); resp = parse_change(buf, parsed); mu_assert("incr foo != receive value error", STR_EQ(resp, "CLIENT_ERROR: didn't receive value\r\n")); prime_buf(buf, "incr foo bar"); resp = parse_change(buf, parsed); mu_assert("incr foo bar != convert error", STR_EQ(resp, "CLIENT_ERROR: value not number\r\n")); prime_buf(buf, "incr foo 14ar"); resp = parse_change(buf, parsed); mu_assert("incr foo 14ar != convert error", STR_EQ(resp, "CLIENT_ERROR: value not number\r\n")); return 0; }
int get_command(pState state){ //magic len command subcommand data checkval //message is len + 8 (2 magic, 2 len, 4 checkval) //magic = 0xa55a, short len, short command, var subcommand, var data, int checkval // unsigned char *buf = malloc(MAX_MESSAGE_LEN+8); unsigned char buf[MAX_MESSAGE_LEN+8]; unsigned int checkval = 0; unsigned int calc_checkval = 0; unsigned short magic = 0; unsigned short len = 0; unsigned short command = 0xaa; unsigned short subcommand = 0xffff; unsigned short sensorID = 0xffff; unsigned int sensor_address = 0xffff; unsigned int coefficient = 0xffff; unsigned short temp = 0xffff; unsigned short numSteps = 0xffff; unsigned short *tempP = NULL; unsigned short seconds = 0; pStep steps = NULL; int ret = -1; unsigned int fw = 0; bzero(buf,MAX_MESSAGE_LEN+8); //get magic get_bytes(buf ,2); magic = get_short(buf); if (magic != MAGIC){ prime_buf(buf); send(buf,12); //free(buf); return 1; } //get len get_bytes(buf+2,2); len = get_short(buf+2); //check len if (len > MAX_MESSAGE_LEN){ prime_buf(buf); send(buf,12); //free(buf); return 1; } //get the rest of message except checkval get_bytes(buf+4, len); //get checkval checkval = get_int(buf+len); //compare to calculated from message calc_checkval = check_val( buf, len); if ( checkval != calc_checkval ){ //bad check_val prime_buf(buf); send(buf,12); //free(buf); return 1; } //get command command = get_short(buf+4); switch (command) { case 1:{ //set power state subcommand = get_short(buf+6); prime_buf(buf); if (subcommand == 0x0000){ power_off(state); } if (subcommand == 0x0001){ power_on(state); } if (subcommand > 0x0001){ //bad subcommand } send(buf, 12); break; } case 2:{//set temp temp = get_short(buf+6); prime_buf(buf); if (set_temp(state,temp) == 2){ buf[6] = 1; } send(buf, 12); break; } case 3:{ //add sensor sensorID = get_short(buf+6); sensor_address = get_int(buf+8); coefficient = get_int(buf+12); unsigned int sensorTemp = get_int(buf+16); //check count < 10 buf[6] = 0x08, sensor_ID is unique/not in use buf[6] = 0x07, ret = add_sensor(state, sensorID, sensor_address, coefficient, sensorTemp); prime_buf(buf); if (ret == 2){buf[6]=0x07;} if (ret == 3){buf[6]=0x08;} send(buf, 12); break; } case 4:{ //remove sensor sensorID = get_short(buf+6); ret = remove_sensor(state,sensorID); prime_buf(buf); if (ret == 1){buf[6]=0x06;} send(buf, 12); break; } case 5:{ //set smoke sensor subcommand = get_short(buf+6); prime_buf(buf); if (subcommand == 0x0000){ smoke_off(state); } if (subcommand == 0x0001){ smoke_on(state); } if (subcommand > 0x0001){ //bad subcommand //no response } send(buf, 12); break; } case 6:{ //set program numSteps = get_short(buf+6); steps = (pStep)(buf+8) ; ret = add_steps(state,numSteps,steps); prime_buf(buf); if (ret == 3){buf[6]=3;} if (ret == 2){buf[6]=2;} if (ret == 1){buf[6]=1;} send(buf, 12); break; } case 7:{//get program prime_buf(buf); unsigned int lenz = 0; unsigned int program[30]; bzero(program,120); buf[4]=1; buf[6]=7; get_program(state, &lenz, program); lenz = lenz*3*sizeof(int); cgc_memcpy(buf+8,&lenz,sizeof(int)); cgc_memcpy(buf+12,program,lenz); send(buf,lenz + 12); break; } case 8:{//get status prime_buf(buf); buf[4]=1; buf[6]=8; unsigned int status[6]; int len = 24; ret = get_status(state,status); cgc_memcpy(buf+8,&len,sizeof(int)); cgc_memcpy(buf+12,status,24); send(buf,36); break; } case 9:{//simulate seconds seconds = get_short(buf+6); prime_buf(buf); unsigned int bufsize = 12; unsigned int histSize = 0; ret = simulate_seconds(state, seconds); unsigned int currentTime = state->currentTime; unsigned int setTemp = state->setTemp; if (ret == 0 ){ buf[6] = 9; cgc_memcpy(buf+8, ¤tTime ,sizeof(int)); send(buf,12); } if(ret == 2){ buf[4] = 1; buf[6] = 0xc; histSize = state->historyPosition*sizeof(int); unsigned int *pHistoryList = state->history; unsigned int historyListSize = state->historyPosition; unsigned int ambientTemp = state->ambientTemp; new_state(state); if (historyListSize > 0){ cgc_memcpy(buf+8, &historyListSize, sizeof(historyListSize)); cgc_memcpy(buf+12, pHistoryList, histSize); bufsize = histSize + 12; } cgc_memcpy(buf+bufsize, &ambientTemp, sizeof(unsigned int)); bufsize+=4; cgc_memcpy(buf+bufsize, &setTemp, sizeof(unsigned int)); bufsize+=4; send(buf, bufsize); //new_state(state); } break; } case 0xa:{ //validate firmware unsigned int fw = validate_fw(state); prime_buf(buf); buf[4]=1; buf[6]=0xa; buf[8]=4; cgc_memcpy(buf+12, &fw,sizeof(int) ); send(buf, 16); break; } case 0xb:{//cgc_read sensor list prime_buf(buf); int len = 0; buf[4]=1; buf[6]=0xb; len = state->sensorCount * (sizeof(int)*4); //buff is filled with sensor bytes unsigned int sensorList[40*sizeof(int)]; get_sensors(state,sensorList); cgc_memcpy(buf+8,&len,sizeof(int)); cgc_memcpy(buf+12,sensorList,len); send(buf, len+12); break; } case 0xc:{//set ambient temp int ambientTemp = get_signed_int(buf+6); prime_buf(buf); if (set_ambient_temp(state,ambientTemp) == 2){ buf[6] = 1; } send(buf, 12); break; } case 0xff:{ //free(buf); exit_normal(); break; } default:{ //bad command prime_buf(buf); buf[6]=5; send(buf,12); break; } } // free(buf); return 0; }
char *test_parse_store() { char *resp, buf[BUFSIZE]; parsed_text *parsed = malloc(sizeof(parsed_text)); prime_buf(buf, "set"); resp = parse_store(buf, parsed); mu_assert("set != key_error", STR_EQ(resp, "CLIENT_ERROR: no key received\r\n")); prime_buf(buf, "set "); resp = parse_store(buf, parsed); mu_assert("set[space] != key_error", STR_EQ(resp, "CLIENT_ERROR: no key received\r\n")); prime_buf(buf, "set foo"); resp = parse_store(buf, parsed); mu_assert("set foo != flags error", STR_EQ(resp, "CLIENT_ERROR: no flags received\r\n")); prime_buf(buf, "set foo 14hi"); resp = parse_store(buf, parsed); mu_assert("set foo 14hi != flag convert error", STR_EQ(resp, "CLIENT_ERROR: flags not number\r\n")); prime_buf(buf, "set foo bar"); resp = parse_store(buf, parsed); mu_assert("set foo 14hi != flag convert error", STR_EQ(resp, "CLIENT_ERROR: flags not number\r\n")); prime_buf(buf, "set foo 17"); resp = parse_store(buf, parsed); mu_assert("set foo 17 != no exptime error", STR_EQ(resp, "CLIENT_ERROR: no exptime received\r\n")); prime_buf(buf, "set foo 17 bar"); resp = parse_store(buf, parsed); mu_assert("set foo 17 bar != exptime convert error", STR_EQ(resp, "CLIENT_ERROR: exptime not number\r\n")); prime_buf(buf, "set foo 17 12"); resp = parse_store(buf, parsed); mu_assert("set foo 17 12 != bytes error", STR_EQ(resp, "CLIENT_ERROR: no bytes (length) received\r\n")); prime_buf(buf, "set foo 17 12 14ar"); resp = parse_store(buf, parsed); mu_assert("set foo 17 12 14ar != bytes error", STR_EQ(resp, "CLIENT_ERROR: bytes not number\r\n")); prime_buf(buf, "set foo 17 12 14"); resp = parse_store(buf, parsed); mu_assert("set foo 17 12 14 != NULL", (resp == NULL) && (!parsed->no_reply)); prime_buf(buf, "set foo 17 12 14 noreply"); resp = parse_store(buf, parsed); mu_assert("set foo 17 12 14 noreply != NULL", ((resp == NULL) && parsed->no_reply)); prime_buf(buf, "set foo 17 12 14 foo"); resp = parse_store(buf, parsed); mu_assert("set foo 17 12 14 foo != tokens error", STR_EQ(resp, "CLIENT_ERROR: too many tokens sent\r\n")); return 0; }