http://wiki.gnokii.org/index.php/Protocol_description
I'm trying to translate the C code found on the website listed above into code for the arduino. If anyone could help point me in the right direction as far as where to start I would appreciate it. I plan to isolate only the code essential for sending and receiving SMS messeges.
Here is the code
/*
some questions or comments send to <zecarlos1957@hotmail.com>
my thanks to web, i am learning width them
my bad ingles!!!!!!!!!!!!????????????
thanks to Justin Karneges who was the ward whork
I am Jose Jesus from Portugal
*/
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <mem.h>
#include <dos.h>
#include <time.h>
#include <string.h>
#include "rs232.h" //or some else serial driver
#define N61MAXSIZE 120
#define BUF_SIZ 1024
typedef unsigned char uchar;
typedef unsigned short ushort;
uchar input_buffer[1024],output_buffer[1024];
unsigned char transtable[]={
'@' ,0xa3,'§' ,0xa5,0xe8,0xe9,0xf9,0xec,
0xf2,0xc7,'\n',0xd8,0xf8,'\r',0xc5,0xe5,
'?' ,'_' ,'?' ,'?' ,'?' ,'?' ,'?' ,'?' ,
'?' ,'?' ,'?' ,'?' ,0xc6,0xe6,0xdf,0xc9,
' ' ,'!' ,'\"','#' ,0xa4,'%' ,'&' ,'\'',
'(' ,')' ,'*' ,'+' ,',' ,'-' ,'.' ,'/' ,
'0' ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,
'8' ,'9' ,':' ,';' ,'<' ,'=' ,'>' ,'?' ,
0xa1,'A' ,'B' ,'C' ,'D' ,'E' ,'F' ,'G' ,
'H' ,'I' ,'J' ,'K' ,'L' ,'M' ,'N' ,'O' ,
'P' ,'Q' ,'R' ,'S' ,'T' ,'U' ,'V' ,'W' ,
'X' ,'Y' ,'Z' ,0xc4,0xd6,0xd1,0xdc,0xa7,
0xbf,'a' ,'b' ,'c' ,'d' ,'e' ,'f' ,'g' ,
'h' ,'i' ,'j' ,'k' ,'l' ,'m' ,'n' ,'o' ,
'p' ,'q' ,'r' ,'s' ,'t' ,'u' ,'v' ,'w' ,
'x' ,'y' ,'z' ,0xe4,0xf6,0xf1,0xfc,0xe0
};
struct Message{
uchar id;
uchar dest;
uchar source;
uchar type;
uchar unknown;
uchar size;
uchar dat[256];
uchar cs[2];
}message;
int firsttime=1;
class TM{
protected:
int seqnum,prevseq,ack,inbox,done,nsms;
uchar memtype;
uchar version[80];
uchar smsc[80];
int sendframe(int,int,uchar*);
int nextseq();
// void sinc();
void smssendfull(char *smsc,char *dest,char *msg);
void sendack(int type,int seqnum);
void bcd(char *dst,char *s);
void bcd2(char *dst,char *s);
// char *unbcd(unsigned char *dat);
// char *unbcd2(unsigned char *dat);
void pack7(char *dst,char *s);
char *unpack7(unsigned char *dat,int len);
unsigned char gettrans(unsigned char c);
int process(Message*);
public:
void addchar(char *str,char c){
int n=strlen(str); str[n]=c; str[n+1]=0;
}
int sendmsg(int type,int len,uchar*buf);
char *unbcd(unsigned char *dat);
char *unbcd2(unsigned char *dat);
TM();
~TM();
void sinc();
int ackwait(int x);
int wait(int x);
int send(uchar *,uchar*);
int update();
int gettxt(uchar*);
void show(Message*);
void ver();
int getmail(){return inbox;}
void savemsg(char *sender,char *d,char *t,char *m);
};
int off=0,mode=0,datp=0,multiple=0,readsize=0;
char Paulo[]={"918617088"};
uchar msg[512];
uchar msg2[512];
uchar Date[10];
uchar Time[10];
char sender[15];
uchar s1[50];
uchar Imei[50];
uchar Operator[50];
uchar SmsC[15];
int NetStatus=0;
int Battery=0;
uchar index=0;
uchar memtype=0x02;
/*
this is a part of a frame.this frame is complet wen sending
to the function TM::sendmsg(getver[0],getver[1],&getver[2])
*/
uchar getver[] = {0xd1,0x05,0x00,0x01,0x00,0x03,0x00};
uchar getsmsc[]= {0x02,0x06,0x00,0x01,0x00,0x33,0x64,0x01};
uchar getimei[]= {0x1b,0x04,0x00,0x01,0x00,0x01};
uchar getbatt[]= {0x17,0x04,0x00,0x01,0x00,0x02};
uchar getrf[] = {0x0a,0x04,0x00,0x01,0x00,0x81};
uchar getnetwork[]={0x0a,0x04,0x00,0x01,0x00,0x70};
uchar readsms[]= {0x14,0x0a,0x00,0x01,0x01,0x07,memtype,0x00,index,0x01,0x65,0x01};
uchar getalarm[]= {0x11,0x04,0x00,0x01,0x00,0x6d};
uchar delsms[] = {0x14,0x08,0x00,0x01,0x00,0x0a,memtype,0x00,index,0x00};
uchar smsstatus[]= {0x14,0x05,0x00,0x01,0x00,0x36,0x64};
class App:public TM{
int status,todo,old;
char port;
uchar *doit;
uchar *tarefa[255];
public:
App():status(0),port('1'),todo(0),old(0){}
int init(){
if( rs_initport(port,RS_B115K,RS_NOPAR,RS_DBIT8,RS_SBIT1,
BUF_SIZ,input_buffer,BUF_SIZ,output_buffer)>0){
status=0;
rs_modctrl(RS_WRTMCR,RS_MCRDTR,RS_LINON);
cprintf("Terminal mode active \r\n");
}
else{
status=1;
cprintf("Unable to open COM port\r\n");
return status;
}
tarefa[6]=::getver;
tarefa[5]=::getsmsc;
tarefa[4]=::getimei;
tarefa[3]=::getbatt;
tarefa[2]=::getrf;
tarefa[1]=::getnetwork;
tarefa[0]=::smsstatus;
todo=7;
return status;
}
~App(){
if(status==0)rs_modctrl(RS_WRTMCR,RS_MCRDTR,RS_LINOFF);
rs_close();
}
int upload(){
int r;
if(todo!=old && todo){
doit=tarefa[todo-1];
sendmsg(doit[0],doit[1],&doit[2]);
old=todo;r=0;
}else{
if(old>0){
printf("Comando [%s] ",itoa((int)doit[0],s1,16));
printf("Nao Executado. Repetir? esc:space\r\n");
old=0;
r=27;
}
}
return r;
}
int getstatus(){
if(done && todo==old && todo ){
todo--;old=0;done=0;
return ((message.type<<8)|message.dat[3]);
}
return 0;
}
void insertcmd(uchar * cmd){
if(todo<255){tarefa[todo++]=cmd;}
else printf("Error to mutch tarefas\r\n");
}
int getarefa(){return todo;}
void sendsms(uchar *dst,uchar *m){
if(smsc==0 || dst==0){
printf("Falta o numero do Centro de mensagens\r\n");
return;
}
smssendfull(smsc,dst,m);
}
void savemsg(uchar *s,uchar *d,uchar *t,uchar *mess){
FILE *f=fopen("backup.dat","at");
if(!f)return;
strcat(s,d);
strcat(s,t);
strcat(s,mess);
fwrite(s,strlen(s),1,f);
fclose(f);
}
};
void main(){
int end=0,c=0,er=0,type,mail=0;
App app;
long t1=0,t2,t3=8000;
char *dst;
if(app.init()==0){
t1=clock();
while(!end){
t2=clock();
if(t2>t1){
t1=t2+t3;
if(app.getarefa()==0){
app.insertcmd(smsstatus);
// see if there is sms to send ? send it
if(app.loadnet());
}
c=app.upload();
}
if(kbhit())c=getch();
if(c){
if(c==27){
while(!kbhit());
if((c=getch())==27)break;
c=0;
}
if(c==0x52){
if(app.gettxt(msg)){
dst=Paulo;
app.sendsms(dst,msg);
}
}
}
delay(1);
if((er=app.update())){
if(er&0x01)printf("Receive overrun(buffer) ");
if(er&0x02)printf("Receive overrun(UART) ");
if(er&0x04)printf("Parity error ");
if(er&0x08)printf("Framing error ");
if(er&0x10)printf("Break detected ");
if(er&0x20)printf("Error in receive FIFO ");
printf(" Esc-ABort : Else-Continue \r\n");
c=27;
}
if((type=app.getstatus())){
if(type==0x1437){
//if status report a sms message
if(index==0)index=message.dat[15];
mail=index;
if(mail ){
printf("%sª Messagem nova ",itoa(mail,s1,10));
readsms[8]=mail;t1=0;
app.insertcmd(readsms); // read it
}
}
if(type==0x1408){
//if there is a sms mark as read
mail=message.dat[7];
if(mail){
printf("%s %s\r\n",sender,::msg);
// app.show(&message);
printf("\r\n");
if(index)index--;
readsms[8]=mail;t1=0;
// app.insertcmd(delsms); // delete it
// app.savemsg(sender,Date,Time,msg);
}
}
if(app.getarefa()>1)t1=0;
}
}
}
}
TM::TM():seqnum(0),prevseq(0),ack(0),memtype(0x02),inbox(0),done(0),nsms(0){
smsc[0]=0;version[0]=0;Imei[0]=0;Operator[0]=0;SmsC[0]=0;
}
TM::~TM(){}
int TM::send(uchar *num,uchar *msg){
if(smsc[0]==0)return 0x300;
printf("Waiting...");
smssendfull(smsc,num,msg);
return 0;
}
int TM::sendmsg(int type,int size,unsigned char *data){
unsigned char buf[N61MAXSIZE+2];
unsigned char num,lastsize;
int n,len,er;
num=(size+N61MAXSIZE-1)/N61MAXSIZE;
lastsize=size%N61MAXSIZE;
sinc();
for(n=0;n<num;++n){
if(n+1==num) len=lastsize;
else len=N61MAXSIZE;
memcpy(buf,data+(n*N61MAXSIZE),len);
buf[len++]=num-n; // multiple framed msg
buf[len++]=nextseq(); // sequenci
if(n) buf[len-1]&=7;
ack=0;
if((er=sendframe(type,len,buf)))break;
}
return er;
}
int TM::sendframe(int type,int size,uchar *data){
unsigned char buf[256];
int at,n,check,len;
unsigned short *p;
at=0;
buf[at++]=0x1e; // id comunication 0x1e cable=0x1e infred=0x1c
buf[at++]=0x00; // dest 0x00=mobil
buf[at++]=0x0c; // source 0x0c=pc
buf[at++]=type; // comand type see below
buf[at++]=0; // multiple sms
buf[at++]=size; // size of data
for(n=0;n<size;n++) buf[at++]=data[n];
if(size%2)buf[at++]=0;
check=0;
p=(unsigned short *)buf;
len=at/2;
for(n=0;n<len;n++) check ^=p[n];
p[n]=check;
at+=2;
int er;
er=rs_sndstr(at,buf);
// printf(" send "); show((Message*)buf);
if(er>0)er|=0x500;
return er;
}