📄 nk2110.c
字号:
return GN_ERR_INVALIDSMSLOCATION; if (SMSData[0] != 0x0b) { ddprintf("No sms there? (%x/%d)\n", SMSData[0], SMSpos); return GN_ERR_EMPTYSMSLOCATION; } ddprintf("Status: " ); switch (SMSData[3]) { case 7: m->Type = GST_MO; m->Status = GSS_NOTSENTREAD; ddprintf("not sent\n"); break; case 5: m->Type = GST_MO; m->Status = GSS_SENTREAD; ddprintf("sent\n"); break; case 3: m->Type = GST_MT; m->Status = GSS_NOTSENTREAD; ddprintf("not read\n"); break; case 1: m->Type = GST_MT; m->Status = GSS_SENTREAD; ddprintf("read\n"); break; } return GN_ERR_NONE);}#endifstatic gn_error DeleteSMSMessage(GSM_SMSMessage *message){ ddprintf("deleting..."); return SMS(message, 3);}/* GetRFLevel */static intGetValue(u8 index, u8 type){ u8 pkt[] = {0x84, 2, 0}; /* Sending 4 at pkt[0] makes phone crash */ int val; pkt[0] = index; pkt[1] = type; PacketOK = false; ddprintf("\nRequesting value(%d)", index); SendCommand(pkt, 0xe5, 3); waitfor(PacketOK, 0); if ((PacketData[3] != 0xe6) || (PacketData[4] != index) || (PacketData[5] != type)) dprintf("Something is very wrong with GetValue\n"); val = PacketData[7]; ddprintf( "Value = %d\n", val ); return (val);}static gn_errorGetRFLevel(GSM_RFUnits *units, float *level){ int val = GetValue(0x84, 2); float res; if (*units == GRF_Arbitrary) { res = (100* (float) val) / 60.0; /* This should be / 99.0 for some models other than nokia-2110 */ *level = 0; if (res > 10) *level = 1; if (res > 30) *level = 2; if (res > 50) *level = 3; if (res > 70) *level = 4; } else { *level = (100* (float) val) / 60.0; /* This should be / 99.0 for some models other than nokia-2110 */ *units = GRF_Percentage; } return GN_ERR_NONE);}static gn_errorGetBatteryLevel(GSM_BatteryUnits *units, float *level){ int val = GetValue(0x85, 2); *level = 0; if (val >= 5) *level = 1; if (val >= 10) *level = 2; if (val >= 90) *level = 3; if (*units == GBU_Arbitrary) { } else {/* *level = (100 * (float) val) / 90.0;*/ /* 5..first bar, 10..second bar, 90..third bar */ *level = *level * 33; *units = GBU_Percentage; } return GN_ERR_NONE);}static gn_error GetVersionInfo(void){ char *s = VersionInfo; ddprintf("Getting version info...\n"); if (GetValue(0x11, 0x03) == -1) return GN_ERR_TIMEOUT; strncpy( s, (void *) &PacketData[6], sizeof(VersionInfo) ); for( Revision = s; *s != 0x0A; s++ ) if( !*s ) return GN_ERR_NONE); *s++ = 0; for( RevisionDate = s; *s != 0x0A; s++ ) if( !*s ) return GN_ERR_NONE); *s++ = 0; for( Model = s; *s != 0x0A; s++ ) if( !*s ) return GN_ERR_NONE); *s++ = 0; ddprintf("Revision %s, Date %s, Model %s\n", Revision, RevisionDate, Model ); return GN_ERR_NONE);}/* Our "Not implemented" functions */static gn_errorGetMemoryStatus(GSM_MemoryStatus *Status){ switch(Status->MemoryType) { case GMT_ME: Status->Used = 0; Status->Free = 150; break; case GMT_LD: Status->Used = 5; Status->Free = 0; break; case GMT_ON: Status->Used = 1; Status->Free = 0; break; case GMT_SM: Status->Used = 0; Status->Free = 150; break; default: return GN_ERR_NOTIMPLEMENTED); } return GN_ERR_NONE);}static boolSendRLPFrame(RLP_F96Frame *frame, bool out_dtx){ return (false);}static char *Display(u8 b, int shift, char *s, char *buf){ b >>= shift; b &= 0x03; switch (b) { case 0: break; case 1: case 2: *buf++ = '!'; case 3: strcpy(buf, s); buf += strlen(s); *buf++ = ' '; } return buf;}static void (*OutputFn)(GSM_DrawMessage *DrawMessage);static gn_error (*OnSMSFn)(GSM_SMSMessage *m);int SMSReady = 0;static intCheckIncomingSMS(int at){ GSM_SMSMessage m; eprintf("Check message at %d\n", at); memset(&m, 0, sizeof(m)); m.Number = at; m.MemoryType = GMT_ME;#if 0 if (GetSMSMessage(&m) != GN_ERR_NONE) { return 0; }#endif#warning Need to do something with data... somehow. if (OnSMSFn) OnSMSFn(&m); DeleteSMSMessage(&m); return 1;}static intHandlePacket(void){ GSM_DrawMessage drawmsg; int i; int lsize[5] = {10, 10, 10, 3, 12}; char *t; eprintf("[%x]", PacketData[3]); switch(PacketData[3]) { case 0x12: { /* Text from display */ drawmsg.Command = GSM_Draw_ClearScreen; if (OutputFn) OutputFn(&drawmsg); t = (char *)&PacketData[8]; for (i = 0; i < 5; i++) { drawmsg.Command = GSM_Draw_DisplayText; drawmsg.Data.DisplayText.x = 0; drawmsg.Data.DisplayText.y = ((i+1)*48)/DRAW_MAX_SCREEN_HEIGHT; strncpy(drawmsg.Data.DisplayText.text, t, lsize[i]); drawmsg.Data.DisplayText.text[ lsize[i] ] = 0; if (OutputFn) OutputFn(&drawmsg); t += lsize[i]; } return 1; } case 0x2f: { /* Display lights */#define COPYFLAG(x, y, z) if (PacketData[x] & (1 << (y))) drawmsg.Data.DisplayStatus |= (1 << (z)) drawmsg.Data.DisplayStatus = 0; drawmsg.Command = GSM_Draw_DisplayStatus; COPYFLAG(7, 2, DS_Unread_SMS); if (OutputFn) OutputFn(&drawmsg);#if 0/* Please convert it. I didn't managed to convert the rest... :-( - bozo */ char buf[10240], *s = buf;#undef COPY#define COPY(x, y, z) s = Display(PacketData[x], y, z, s) /* Valid for 2110 */ COPY(4, 0, "d"); COPY(4, 2, "b"); COPY(4, 4, "a"); COPY(4, 6, "lights"); COPY(5, 0, "service"); COPY(5, 2, "scroll_up"); COPY(5, 4, "scroll_down"); COPY(5, 6, "ABC"); COPY(6, 0, "2.>"); COPY(6, 2, "1.>"); COPY(6, 4, "roam"); COPY(6, 6, "handset"); COPY(7, 0, "vmail"); COPY(7, 2, "envelope"); COPY(7, 4, "battbar"); COPY(7, 6, "3.>"); COPY(8, 0, "?1"); COPY(8, 2, "?2"); COPY(8, 4, "fieldbar"); COPY(8, 6, "ring"); *s++ = 0; if (OutputFn) (*OutputFn)(NULL, buf); return 1;#undef COPY#endif } case LM_SMS_EVENT: /* SMS Data */ /* copy bytes 5+ to smsbuf */ ddprintf("SMSdata:"); { int i; for (i=5; i<PacketData[2]+4; i++) { SMSData[SMSpos++] = PacketData[i]; ddprintf("%c", PacketData[i]); } fflush(stdout); } /* Handle incoming sms notifications */ { switch (SMSData[0]) { case LM_SMS_RECEIVED_PP_DATA: eprintf("Data came!\n"); SMSpos = 0; return 1; case LM_SMS_ALIVE_TEST: eprintf("Am I alive?\n"); SMSpos = 0; return 1; case LM_SMS_NEW_MESSAGE_INDICATION: { int i, at; SMSpos = 0; at = SMSData[2]; eprintf("New message indicated @%d\n", at); for (i=0; i<sizeof(SMSData); i++) SMSData[i] = 0; SMSReady = at; return 1; } } } return ((PacketData[4] & 0xf) != 0); /* Make all but last fragment "secret" */ default: dprintf("Unknown response %dx\n", PacketData[3]); return 0; }}/* Handler called when characters received from serial port. * and process them. */static voidSigHandler(int status){ unsigned char buffer[256], ack[5], b; int i, res; static unsigned int Index = 0, Length = 5; static unsigned char pkt[256]; int j; res = device_read(buffer, 256); if( res < 0 ) return; for(i = 0; i < res ; i++) { b = buffer[i];/* dprintf("(%x)", b, Index); */ if (!Index && b != MYID && b != 0xf8 && b != 0x00) /* MYID is code of computer */ { /* something strange goes from phone. Just ignore it */ ddprintf("Get [%02X %c]\n", b, b >= 0x20 ? b : '.' ); continue; } else { pkt[Index++] = b; if(Index == 3) { Length = b + 6; if (b == 0x7f) Length = 5; if (b == 0x7e) Length = 8; } if(Index >= Length) { if((pkt[0] == MYID || pkt[0]==0xf8) && pkt[1] == 0x00) /* packet from phone */ { ddprintf("Phone: "); for( j = 0; j < Length; j++ ) { b = pkt[j]; ddprintf( "[%02X %c]", b, b >= 0x20 ? b : '.' ); } ddprintf( "\n" ); /* ensure that we received valid packet */ if(pkt[Length - 1] != GetChecksum(pkt, Length-1)) { eprintf( "***bad checksum***"); } else { if((pkt[2] == 0x7F) || (pkt[2] == 0x7E)) /* acknowledge by phone */ { if (pkt[2] == 0x7F) { dprintf( "[ack]" ); /* Set ACKOK flag */ ACKOK = true; /* Increase TX packet number */ } else { dprintf( "[registration ack]" ); N2110_LinkOK = true; } TXPacketNumber++; } else { /* Copy packet data */ dprintf( "[data]" ); memcpy((void *) PacketData,pkt,Length); /* send acknowledge packet to phone */ msleep(10); ack[0] = 0x00; /* Construct the header. */ ack[1] = pkt[0]; /* Send back id */ ack[2] = 0x7F; /* Set special size value */ ack[3] = pkt[Length - 2]; /* Send back packet number */ ack[4] = GetChecksum( ack, 4); /* Set checksum */ ddprintf( _("PC : ") ); for( j = 0; j < 5; j++ ) { b = ack[j]; ddprintf( "[%02X %c]", b, b >= 0x20 ? b : '.' ); } ddprintf( "\n" ); LastChar = GetTime(); if( xwrite( ack, 5 ) == -1 ) perror( _("Write error!\n") ); if( xread( ack, 5 ) == -1 ) perror( _("Read ack error!\n") ); /* Set validity flag */ if (!HandlePacket()) PacketOK = true; } } } else eprintf("Got my own echo? That should not be possible!\n"); /* Look for new packet */ Index = 0; Length = 5; } } } if (SMSReady) { int at = SMSReady; SMSReady = 0; if (!CheckIncomingSMS(at)) eprintf("Could not find promissed message?\n"); }}/* Called by initialisation code to open comm port in asynchronous mode. */bool OpenSerial(void){ int result; ddprintf(_("Setting MBUS communication with 2110...\n")); result = device_open(PortDevice, true, false, false, GCT_Serial); if (!result) { fprintf(stderr, "Failed to open %s ...\n", PortDevice); return (false); } ddprintf("%s opened...\n", PortDevice); device_changespeed(9600); device_setdtrrts(1, 1); return (true);}static gn_errorSetKey(int c, int up){ u8 reg[] = { 0x7a /* RPC_UI_KEY_PRESS or RPC_UI_KEY_RELEASE */, 0, 1, 0 /* key code */ }; reg[0] += up; reg[3] = c; dprintf("\n Pressing %d\n", c ); PacketOK = false; SendCommand( reg, 0xe5, 4 ); waitfor(PacketOK, 0); return GN_ERR_NONE;}#define XCTRL(a) (a&0x1f)#define POWER XCTRL('o')#define SEND XCTRL('t')#define END XCTRL('s')#define CLR XCTRL('h')#define MENU XCTRL('d')#define ALPHA XCTRL('a')#define PLUS XCTRL('b')#define MINUS XCTRL('e')#define PREV XCTRL('p')#define NEXT XCTRL('n')#define SOFTA XCTRL('x')#define SOFTB XCTRL('q')static char lastkey;static void PressKey(char c, int i){ lastkey = c;#define X( a, b ) case a: SetKey(b, i); break; switch (c) { case '1' ... '9': SetKey(c-'0',i); break; X('0', 10) X('#', 11) X('*', 12) X(POWER, 13) X(SEND, 14) X(END, 15) X(PLUS, 16) X(MINUS, 17) X(CLR, 18) X(MENU, 21)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -