libJelling-calc/src/module.cpp

37 lines
985 B
C++

#include "libJelling/module.h"
int Module::setName(char *string){
char buffer[28] = "AT+NAME";
if(strlen(string) > 20) return TOO_LONG;
strcat(buffer,(const char*)string);
return set((unsigned char*)buffer);
}
int Module::setPassword(char *string){
char buffer[11] = "AT+PIN";
if(strlen(string) > 4) return TOO_LONG;
else if(strlen(string) < 4) return TOO_SHORT;
strcat(buffer, (const char*)string);
return set((unsigned char*)buffer);
}
int Module::set(unsigned char *string){
int RTCTime;
unsigned char conf[6] = {0, 5, 0, 0, 0, 0};
unsigned char buffer[2];
short *size = NULL;
Serial_Open(conf);
Serial_ClearReceiveBuffer();
if(Serial_WriteBytes(string, strlen((const char*)string)+1)) return WRITE_ERROR;
RTCTime = RTC_GetTicks();
while(Serial_GetRxBufferSize()<2){
if((RTC_GetTicks() - RTCTime)/RTC > 1000){
return BAD_RESPONSE;
}
}
Serial_ReadBytes(buffer, 2, size);
if(buffer[0] == 'O' && buffer[1] == 'K') return 0;
else return BAD_RESPONSE;
}