http://marcin-wiacek.fkn.pl/english/zips/mygnokii.tar.gz
[gnokii.git] / common / newmodules / n7110.c
index b1c9c7e..e260cfd 100644 (file)
@@ -78,7 +78,7 @@ GSM_Functions N7110_Functions = {
   N7110_SetAlarm,
   N6110_DialVoice,
   N6110_DialData,
-  N6110_GetIncomingCallNr,
+  N6110_GetIncomingCallNr,\r
   N6110_GetNetworkInfo,
   N7110_GetCalendarNote,
   N7110_WriteCalendarNote,
@@ -250,7 +250,6 @@ GSM_Error N7110_Initialise(char *port_device, char *initlength,
   }
   
   CurrentSMSFoldersCount=1000;
-  CurrentSMSMessage=NULL;
 
   return (GE_NONE);
 }
@@ -727,6 +726,10 @@ GSM_Error N7110_GetWAPSettings (GSM_WAPSettings *settings)
 
 void N7110_ReplyGetMemoryStatus(u16 MessageLength, u8 *MessageBuffer, u8 MessageType)
 {
+#ifdef DEBUG
+  fprintf(stdout, _("Message: Memory status received:\n"));
+#endif
+
   if (CurrentMemoryStatus && CurrentMemoryStatusError == GE_BUSY) {
     /* first Loc. (MessageBuffer[10]<<8) + MessageBuffer[11]; */
     CurrentMemoryStatus->Free = (MessageBuffer[14]<<8) + MessageBuffer[15];
@@ -736,8 +739,6 @@ void N7110_ReplyGetMemoryStatus(u16 MessageLength, u8 *MessageBuffer, u8 Message
     CurrentMemoryStatusError = GE_NONE;
 
 #ifdef DEBUG
-    fprintf(stdout, _("Message: Memory status received:\n"));
-
     fprintf(stdout, _("   Memory Type: %s\n"), N7110_MemoryType_String[MessageBuffer[5]]);
     fprintf(stdout, _("   Used: %d\n"), CurrentMemoryStatus->Used);
     fprintf(stdout, _("   Free: %d\n"), CurrentMemoryStatus->Free);
@@ -1155,7 +1156,7 @@ GSM_Error N7110_GetCalendarNote(GSM_CalendarNote *CalendarNote)
     return GE_INVALIDCALNOTELOCATION;
   
   req[4] = CurrentCalendarNotesInfo.Location[CalendarNote->Location-1]>>8;
-  req[5] = CurrentCalendarNotesInfo.Location[CalendarNote->Location-1]&0xff;
+  req[5] = CurrentCalendarNotesInfo.Location[CalendarNote->Location-1]&0xff;\r
   CurrentCalendarNote = CalendarNote;
 
   /* We have to get current year. It's NOT written in frame for Birthday */
@@ -2084,203 +2085,203 @@ GSM_Error N7110_GetSMSStatus(GSM_SMSStatus *Status)
   return NULL_SendMessageSequence(10, &CurrentSMSStatusError, 5, 0x14, req);
 }
 
-void N7110_ReplyGetMemoryLocation(u16 MessageLength, u8 *MessageBuffer, u8 MessageType)
+void N7110_DecodePhonebookFrame(GSM_PhonebookEntry *entry,u8 *MessageBuffer,u16 MessageLength)
 {
-  int i, count, blocks, blockcount;
-#ifdef DEBUG
-  int j;
-#endif
+  int blockcount=0;
   unsigned char *pBlock;
-
-  CurrentPhonebookEntry->Empty = true;
-  CurrentPhonebookEntry->Group = 5;     /* 5 = no group as 6110 */
-  CurrentPhonebookEntry->Name[0] = '\0';
-  CurrentPhonebookEntry->Number[0] = '\0';
-  CurrentPhonebookEntry->SubEntriesCount = 0;
+  int length=0;
 
 #ifdef DEBUG
-  fprintf(stdout, _("Message: Phonebook entry received:\n"));
-#endif
-
-  if( MessageBuffer[6] == 0x0f ) // not found
-  {
-#ifdef DEBUG
-    fprintf(stdout, _("   Error %i\n"),MessageBuffer[10]);
-    switch (MessageBuffer[10]) {
-      case 0x34:fprintf(stdout,_("   Invalid phonebook location\n"));break;
-      case 0x3b:fprintf(stdout,_("   Speed dial not assigned\n"));break;
-      default  :fprintf(stdout,_("   Unknown.Please report\n"));break;
-    }
+  int j;
 #endif
-    switch (MessageBuffer[10]) {
-      case 0x34:CurrentPhonebookError = GE_INVALIDPHBOOKLOCATION;break;
-      case 0x3b:CurrentPhonebookError = GE_INVALIDSPEEDDIALLOCATION;break;
-      default  :CurrentPhonebookError = GE_UNKNOWN;
-    }
-    CurrentSpeedDialError=GE_INVALIDSPEEDDIALLOCATION;
-
-  } else {
-
-    CurrentPhonebookEntry->Location=MessageBuffer[13];
 
-    count      = MessageBuffer[9];
-    blocks     = MessageBuffer[17];
-    blockcount = 0;
+  pBlock = &MessageBuffer[0];
 
-    CurrentPhonebookEntry->SubEntriesCount = blocks - 1;
+  while (length!=MessageLength) {
+    GSM_SubPhonebookEntry* pEntry = &entry->SubEntries[blockcount];
 
 #ifdef DEBUG
-    fprintf(stdout, _("  Blocks: %d\n"),blocks);
-#endif /* DEBUG */
-
-    pBlock = &MessageBuffer[18];
-
-    for( i = 0; i < blocks; i++ )
-    {
-      GSM_SubPhonebookEntry* pEntry = &CurrentPhonebookEntry->SubEntries[blockcount];
-
-#ifdef DEBUG
-      fprintf(stdout,_("   "));
-      for (j=5;j<(pBlock[3]-6)+5;j++) fprintf(stdout,_("%02x "),pBlock[j]);
-      fprintf(stdout,_("\n"));
+    fprintf(stdout,_("    "));
+    for (j=5;j<(pBlock[3]-6)+5;j++) fprintf(stdout,_("%02x "),pBlock[j]);
+    fprintf(stdout,_("\n"));
 #endif
 
-      switch( pBlock[0] ) {
-      case N7110_ENTRYTYPE_SPEEDDIAL:
+    switch( pBlock[0] ) {
+    case N7110_ENTRYTYPE_SPEEDDIAL:
     
-        CurrentSpeedDialEntry->MemoryType = GMT_SM;
-        if (pBlock[4]==0x02) CurrentSpeedDialEntry->MemoryType = GMT_ME;
+      CurrentSpeedDialEntry->MemoryType = GMT_SM;
+      if (pBlock[4]==0x02) CurrentSpeedDialEntry->MemoryType = GMT_ME;
       
-        CurrentSpeedDialEntry->Location = pBlock[7]+pBlock[6]*256;
+      CurrentSpeedDialEntry->Location = pBlock[7]+pBlock[6]*256;
 
 #ifdef DEBUG
-        fprintf(stdout, _("   Speed dial\n"));
-        fprintf(stdout, _("     Location: %d\n"), CurrentSpeedDialEntry->Location);
-        fprintf(stdout, _("     MemoryType: %i\n"), CurrentSpeedDialEntry->MemoryType);
-        fprintf(stdout, _("     Number: %d\n"), CurrentSpeedDialEntry->Number);
+      fprintf(stdout, _("    Speed dial\n"));
+      fprintf(stdout, _("      Location: %d\n"), CurrentSpeedDialEntry->Location);
+      fprintf(stdout, _("      MemoryType: %i\n"), CurrentSpeedDialEntry->MemoryType);
+      fprintf(stdout, _("      Number: %d\n"), CurrentSpeedDialEntry->Number);
 #endif /* DEBUG */
 
-       CurrentSpeedDialError=GE_NONE;
-        break;
+      CurrentSpeedDialError=GE_NONE;
+      break;
 
-      case N7110_ENTRYTYPE_NAME:
-        DecodeUnicode (CurrentPhonebookEntry->Name, pBlock+6, pBlock[5]/2);
-        if (CurrentGetBitmap && CurrentGetBitmapError == GE_BUSY)
-          strncpy(CurrentGetBitmap->text,CurrentPhonebookEntry->Name,sizeof(CurrentGetBitmap->text));
-        CurrentPhonebookEntry->Empty = false;
+    case N7110_ENTRYTYPE_NAME:
+      DecodeUnicode (entry->Name, pBlock+6, pBlock[5]/2);
+      if (CurrentGetBitmap && CurrentGetBitmapError == GE_BUSY)
+        strncpy(CurrentGetBitmap->text,entry->Name,sizeof(CurrentGetBitmap->text));
+      entry->Empty = false;
 #ifdef DEBUG
-        fprintf(stdout, _("   Name:\n"));
-        fprintf(stdout, _("     Name: %s\n"), CurrentPhonebookEntry->Name);
+      fprintf(stdout, _("    Name:\n"));
+      fprintf(stdout, _("      Name: %s\n"), entry->Name);
 #endif /* DEBUG */
-        break;
+      break;
 
-      case N7110_ENTRYTYPE_NUMBER:
-        pEntry->EntryType   = pBlock[0];
-        pEntry->NumberType  = pBlock[5];
-        pEntry->BlockNumber = pBlock[4];
-
-        DecodeUnicode (pEntry->data.Number, pBlock+10, pBlock[9]/2);
-
-#ifdef DEBUG
-        fprintf(stdout, _("   Number:\n"));
-        fprintf(stdout, _("     Type: %d (%02x)\n"),
-          pEntry->NumberType,
-          pEntry->NumberType);
-        fprintf(stdout, _("     Number: %s\n"),
-          pEntry->data.Number);
-#endif /* DEBUG */
-        if( pEntry->EntryType == GSM_Number &&
-            ((pEntry->NumberType == GSM_General &&
-             !strcmp(CurrentPhonebookEntry->Number,""))
-              || pEntry->NumberType == GSM_SIM)) {
-          strcpy( CurrentPhonebookEntry->Number, pEntry->data.Number );
-          *pEntry->data.Number = 0;
-        } else
-          blockcount++;
-        break;
-      case N7110_ENTRYTYPE_DATE:
-        pEntry->EntryType        = pBlock[0];
-        pEntry->NumberType       = pBlock[5];
-        pEntry->BlockNumber      = pBlock[4];
-        DecodeDateTime(pBlock+6, &pEntry->data.Date);
-#ifdef DEBUG
-        fprintf(stdout, _("   Date:\n"));
-        fprintf(stdout, _("     Date: %02u.%02u.%04u\n"), pEntry->data.Date.Day,
-          pEntry->data.Date.Month, pEntry->data.Date.Year );
-        fprintf(stdout, _("     Time: %02u:%02u:%02u\n"), pEntry->data.Date.Hour,
-          pEntry->data.Date.Minute, pEntry->data.Date.Second);
+    case N7110_ENTRYTYPE_NUMBER:
+      pEntry->EntryType   = pBlock[0];
+      pEntry->NumberType  = pBlock[5];
+      pEntry->BlockNumber = pBlock[4];
+
+      DecodeUnicode (pEntry->data.Number, pBlock+10, pBlock[9]/2);
+
+#ifdef DEBUG
+      fprintf(stdout, _("    Number:\n"));
+      fprintf(stdout, _("      Type: %d (%02x)\n"),pEntry->NumberType,pEntry->NumberType);
+      fprintf(stdout, _("      Number: %s\n"),pEntry->data.Number);
 #endif /* DEBUG */
+      if( pEntry->EntryType == GSM_Number &&
+         ((pEntry->NumberType == GSM_General && !strcmp(entry->Number,""))
+            || pEntry->NumberType == GSM_SIM)) {
+        strcpy( entry->Number, pEntry->data.Number );
+        *pEntry->data.Number = 0;
+      } else
         blockcount++;
-        break;
-      case N7110_ENTRYTYPE_NOTE:
-      case N7110_ENTRYTYPE_POSTAL:
-      case N7110_ENTRYTYPE_EMAIL:
-        pEntry->EntryType   = pBlock[0];
-        pEntry->NumberType  = 0;
-        pEntry->BlockNumber = pBlock[4];
+      break;
+    case N7110_ENTRYTYPE_DATE:
+      pEntry->EntryType        = pBlock[0];
+      pEntry->NumberType       = pBlock[5];
+      pEntry->BlockNumber      = pBlock[4];
+      DecodeDateTime(pBlock+6, &pEntry->data.Date);
+#ifdef DEBUG
+      fprintf(stdout, _("    Date:\n"));
+      fprintf(stdout, _("      Date: %02u.%02u.%04u\n"), pEntry->data.Date.Day,
+        pEntry->data.Date.Month, pEntry->data.Date.Year );
+      fprintf(stdout, _("      Time: %02u:%02u:%02u\n"), pEntry->data.Date.Hour,
+        pEntry->data.Date.Minute, pEntry->data.Date.Second);
+#endif /* DEBUG */
+      blockcount++;
+      break;
+    case N7110_ENTRYTYPE_NOTE:
+    case N7110_ENTRYTYPE_POSTAL:
+    case N7110_ENTRYTYPE_EMAIL:
+      pEntry->EntryType   = pBlock[0];
+      pEntry->NumberType  = 0;
+      pEntry->BlockNumber = pBlock[4];
 
-        DecodeUnicode (pEntry->data.Number, pBlock+6, pBlock[5]/2);
+      DecodeUnicode (pEntry->data.Number, pBlock+6, pBlock[5]/2);
 
 #ifdef DEBUG
-        fprintf(stdout, _("   Email or note or postal:\n"));
-        fprintf(stdout, _("     Type: %d (%02x)\n"),
-          pEntry->EntryType,
-          pEntry->EntryType);
-        fprintf(stdout, _("     Text: %s\n"),
-          pEntry->data.Number);
+      fprintf(stdout, _("    Email or note or postal:\n"));
+      fprintf(stdout, _("      Type: %d (%02x)\n"),pEntry->EntryType,pEntry->EntryType);
+      fprintf(stdout, _("      Text: %s\n"),pEntry->data.Number);
 #endif /* DEBUG */
-        blockcount++;
-        break;
-      case N7110_ENTRYTYPE_GROUP:
-        CurrentPhonebookEntry->Group = pBlock[5]-1;  /* 0 = family as for 6110 */
-        if (CurrentGetBitmap && CurrentGetBitmapError == GE_BUSY)
-          CurrentGetBitmap->number = CurrentPhonebookEntry->Group;
+      blockcount++;
+      break;
+    case N7110_ENTRYTYPE_GROUP:
+      entry->Group = pBlock[5]-1;  /* 0 = family as for 6110 */
+      if (CurrentGetBitmap && CurrentGetBitmapError == GE_BUSY)
+        CurrentGetBitmap->number = entry->Group;
 #ifdef DEBUG
-        fprintf(stdout, _("   Group: %d\n"), CurrentPhonebookEntry->Group);
+      fprintf(stdout, _("    Group: %d\n"), entry->Group);
 #endif /* DEBUG */
-        break;
-      case N7110_ENTRYTYPE_RINGTONE:
-        if (CurrentGetBitmap && CurrentGetBitmapError == GE_BUSY)
-          CurrentGetBitmap->ringtone = pBlock[5];
+      break;
+    case N7110_ENTRYTYPE_RINGTONE:
+      if (CurrentGetBitmap && CurrentGetBitmapError == GE_BUSY)
+        CurrentGetBitmap->ringtone = pBlock[5];
 #ifdef DEBUG
-        fprintf(stdout, _("   Group ringtone number %d received.\n"), pBlock[5]);
+      fprintf(stdout, _("    Group ringtone number %d received.\n"), pBlock[5]);
 #endif /* DEBUG */
-        break;
-      case N7110_ENTRYTYPE_LOGOON:
-        if (CurrentGetBitmap && CurrentGetBitmapError == GE_BUSY)
-          CurrentGetBitmap->enabled = pBlock[5];
+      break;
+    case N7110_ENTRYTYPE_LOGOON:
+      if (CurrentGetBitmap && CurrentGetBitmapError == GE_BUSY)
+        CurrentGetBitmap->enabled = pBlock[5];
 #ifdef DEBUG
-        fprintf(stdout, _("   Logo enabled = %d received.\n"), pBlock[5]);
+      fprintf(stdout, _("    Logo enabled = %d received.\n"), pBlock[5]);
 #endif /* DEBUG */
-        break;
-      case N7110_ENTRYTYPE_GROUPLOGO:
+      break;
+    case N7110_ENTRYTYPE_GROUPLOGO:
 #ifdef DEBUG
-        fprintf(stdout, _("   Caller group logo received.\n"));
+      fprintf(stdout, _("    Caller group logo received.\n"));
 #endif /* DEBUG */
-        if (CurrentGetBitmap && CurrentGetBitmapError == GE_BUSY) {
-          CurrentGetBitmap->width = pBlock[5];
-          CurrentGetBitmap->height= pBlock[6];
-          CurrentGetBitmap->size  = pBlock[9];
-          if (CurrentGetBitmap->size > sizeof(CurrentGetBitmap->bitmap))
-            CurrentGetBitmap->size = CurrentGetBitmap->size;
-          memcpy(CurrentGetBitmap->bitmap,pBlock+10,CurrentGetBitmap->size);
-        }
-        break;
+      if (CurrentGetBitmap && CurrentGetBitmapError == GE_BUSY) {
+        CurrentGetBitmap->width = pBlock[5];
+        CurrentGetBitmap->height= pBlock[6];
+        CurrentGetBitmap->size  = pBlock[9];
+        if (CurrentGetBitmap->size > sizeof(CurrentGetBitmap->bitmap))
+          CurrentGetBitmap->size = CurrentGetBitmap->size;
+        memcpy(CurrentGetBitmap->bitmap,pBlock+10,CurrentGetBitmap->size);
+      }
+      break;
 
-      default:
+    default:
 #ifdef DEBUG
-        fprintf(stdout, _("   Unknown Entry Code (%u) received.\n"), pBlock[0] );
+      fprintf(stdout, _("    Unknown Entry Code (%u) received.\n"), pBlock[0] );
 #endif /* DEBUG */
-        break;
-      }
+      break;
+    }
        
 #ifdef DEBUG
-      fprintf(stdout, _("     Blocksize was: %d (%02x)\n"), (int) pBlock[3], pBlock[3]);
+    fprintf(stdout, _("    Blocksize was: %d (%02x)\n"), (int) pBlock[3], pBlock[3]);
+#endif
+    length=length+pBlock[3];
+
+    pBlock = &pBlock[(int) pBlock[3]];
+  }
+
+  entry->SubEntriesCount = blockcount;
+
+#ifdef DEBUG
+  fprintf(stdout, _("  SubBlocks: %d\n"),entry->SubEntriesCount);
+#endif /* DEBUG */
+}
+
+void N7110_ReplyGetMemoryLocation(u16 MessageLength, u8 *MessageBuffer, u8 MessageType)
+{
+
+  CurrentPhonebookEntry->Empty = true;
+  CurrentPhonebookEntry->Group = 5;     /* 5 = no group as 6110 */
+  CurrentPhonebookEntry->Name[0] = '\0';
+  CurrentPhonebookEntry->Number[0] = '\0';
+  CurrentPhonebookEntry->SubEntriesCount = 0;
+
+#ifdef DEBUG
+  fprintf(stdout, _("Message: Phonebook entry received:\n"));
+#endif
+
+  if( MessageBuffer[6] == 0x0f ) // not found
+  {
+#ifdef DEBUG
+    fprintf(stdout, _("   Error %i\n"),MessageBuffer[10]);
+    switch (MessageBuffer[10]) {
+      case 0x34:fprintf(stdout,_("   Invalid phonebook location\n"));break;
+      case 0x3b:fprintf(stdout,_("   Speed dial not assigned\n"));break;
+      default  :fprintf(stdout,_("   Unknown.Please report\n"));break;
+    }
 #endif
-      pBlock = &pBlock[(int) pBlock[3]];
+    switch (MessageBuffer[10]) {
+      case 0x34:CurrentPhonebookError = GE_INVALIDPHBOOKLOCATION;break;
+      case 0x3b:CurrentPhonebookError = GE_INVALIDSPEEDDIALLOCATION;break;
+      default  :CurrentPhonebookError = GE_UNKNOWN;
     }
+    CurrentSpeedDialError=GE_INVALIDSPEEDDIALLOCATION;
+
+  } else {
+    CurrentPhonebookEntry->Location=MessageBuffer[13]+MessageBuffer[12]*256;
+
+#ifdef DEBUG
+    fprintf(stdout,_("  Location: %i\n"),CurrentPhonebookEntry->Location);
+#endif
+
+    N7110_DecodePhonebookFrame(CurrentPhonebookEntry,MessageBuffer+18,MessageLength-18);
 
-    CurrentPhonebookEntry->SubEntriesCount = blockcount;
     CurrentPhonebookError = GE_NONE;
   }
 }
@@ -2335,7 +2336,7 @@ void N7110_ReplyWritePhonebookLocation(u16 MessageLength, u8 *MessageBuffer, u8
 int N7110_PackPBKBlock(int id, int size, int no, unsigned char *buf, unsigned char *block)
 {
 #ifdef DEBUG
-  fprintf(stdout,_("Adding block id:%i,length:%i\n"),no+1,size+6);
+  fprintf(stdout,_("Adding block id:%i,number:%i,length:%i\n"),id,no+1,size+6);
 #endif
 
   *(block++) = id;
@@ -2352,20 +2353,18 @@ int N7110_PackPBKBlock(int id, int size, int no, unsigned char *buf, unsigned ch
   return (size + 6);
 }
 
-int N7110_MakePhonebookFrame(unsigned char *req, GSM_PhonebookEntry entry, int *block)
+int N7110_EncodePhonebookFrame(unsigned char *req, GSM_PhonebookEntry entry, int *block2)
 {
-  int count=0, len, i;
+  int count=0, len, i, block=0;
 
   char string[500];
 
-  *block=0;
-
   /* Name */
   len = strlen(entry.Name);
   string[0] = len * 2;       // Length ot the string (without Termination)
   EncodeUnicode((string + 1), entry.Name, len);
   string[len * 2 + 1] = 0;   // Terminating 0
-  count += N7110_PackPBKBlock(N7110_ENTRYTYPE_NAME, len * 2 + 2, *block++, string, req + count);
+  count += N7110_PackPBKBlock(N7110_ENTRYTYPE_NAME, len * 2 + 2, block++, string, req + count);
 
   if (*entry.Number) {
     len = strlen(entry.Number);
@@ -2374,7 +2373,7 @@ int N7110_MakePhonebookFrame(unsigned char *req, GSM_PhonebookEntry entry, int *
     string[4] = len * 2;     // length (without Termination)
     EncodeUnicode((string + 5), entry.Number, len);
     string[len * 2 + 5] = 0; // Terminating 0
-    count += N7110_PackPBKBlock(N7110_ENTRYTYPE_NUMBER, len * 2 + 6, *block++, string, req + count);
+    count += N7110_PackPBKBlock(N7110_ENTRYTYPE_NUMBER, len * 2 + 6, block++, string, req + count);
   }
                
   /* Rest of the subentries */
@@ -2384,14 +2383,14 @@ int N7110_MakePhonebookFrame(unsigned char *req, GSM_PhonebookEntry entry, int *
       string[0] = len * 2;     // length (without Termination)
       EncodeUnicode((string + 1), entry.SubEntries[i].data.Number, len);
       string[len * 2 + 1] = 0; // Terminating 0
-      count += N7110_PackPBKBlock(entry.SubEntries[i].EntryType, len * 2 + 2, *block++, string, req + count);
+      count += N7110_PackPBKBlock(entry.SubEntries[i].EntryType, len * 2 + 2, block++, string, req + count);
     } else {
       string[0] = entry.SubEntries[i].NumberType;
       string[1] = string[2] = string[3] = 0;
       string[4] = len * 2;     //length (without Termination)
       EncodeUnicode((string + 5), entry.SubEntries[i].data.Number, len);
       string[len * 2 + 5] = 0; // Terminating 0
-      count += N7110_PackPBKBlock(N7110_ENTRYTYPE_NUMBER, len * 2 + 6, *block++, string, req + count);
+      count += N7110_PackPBKBlock(N7110_ENTRYTYPE_NUMBER, len * 2 + 6, block++, string, req + count);
     }
   } 
 
@@ -2399,9 +2398,11 @@ int N7110_MakePhonebookFrame(unsigned char *req, GSM_PhonebookEntry entry, int *
     /* Group */
     string[0] = entry.Group + 1;
     string[1] = 0;
-    count += N7110_PackPBKBlock(N7110_ENTRYTYPE_GROUP, 2, *block++, string, req + count);
+    count += N7110_PackPBKBlock(N7110_ENTRYTYPE_GROUP, 2, block++, string, req + count);
   }
 
+  *block2=block;
+
   return count;
 }
  
@@ -2419,12 +2420,9 @@ GSM_Error N7110_WritePhonebookLocation(GSM_PhonebookEntry *entry)
     req[12] = (entry->Location >> 8);
     req[13] = entry->Location & 0xff;
 
-    // If we had a file that contain data in entry style like from 6110
-    // we can import this too :)
-    // no SubEntries in SIM (besides Name, Number, and Group)
-    if (entry->MemoryType == GMT_SM) entry->SubEntriesCount = 0; 
+//    if (entry->MemoryType == GMT_SM) entry->SubEntriesCount = 0; 
 
-    count=count+N7110_MakePhonebookFrame(req+18, *entry, &blocks);
+    count=count+N7110_EncodePhonebookFrame(req+18, *entry, &blocks);
 
     req[17]=blocks;
 
@@ -2503,6 +2501,15 @@ GSM_Error N7110_WriteGroupDataLocation(GSM_Bitmap *bitmap)
   string[1] = 0;
   count += N7110_PackPBKBlock(N7110_ENTRYTYPE_GROUP, 2, block++, string, req + count);
 
+  /* Name */
+  if (*bitmap->text) {
+    i = strlen(bitmap->text);
+    string[0] = i * 2 + 2;
+    EncodeUnicode((string + 1), bitmap->text, i);
+    string[i * 2 + 1] = 0;   // Terminating 0
+    count += N7110_PackPBKBlock(N7110_ENTRYTYPE_NAME, i * 2 + 2, block++, string, req + count);
+  }
+
   /* Logo */
   string[0] = bitmap->width;
   string[1] = bitmap->height;
@@ -2512,14 +2519,6 @@ GSM_Error N7110_WriteGroupDataLocation(GSM_Bitmap *bitmap)
   memcpy(string + 5, bitmap->bitmap, bitmap->size);
   count += N7110_PackPBKBlock(N7110_ENTRYTYPE_GROUPLOGO, bitmap->size + 5, block++, string, req + count);
 
-  /* Name */
-  if (*bitmap->text) {
-    i = strlen(bitmap->text);
-    string[0] = i * 2;
-    EncodeUnicode((string + 1), bitmap->text, i);
-    count += N7110_PackPBKBlock(N7110_ENTRYTYPE_NAME, i * 2 + 1, block++, string, req + count);
-  }
-
   req[17] = block; //number of blocks
 
   return NULL_SendMessageSequence
@@ -2657,11 +2656,11 @@ GSM_Error N7110_SetBitmap(GSM_Bitmap *Bitmap)
     /* set logo */
     if (strcmp(Bitmap->netcode,"000 00")) {
       reqOp[5] = 0x01;      // Logo enabled
-      EncodeNetworkCode(reqOp+5, Bitmap->netcode);
-      reqOp[11] = 8+(Bitmap->width*Bitmap->height+7)/8;
+      EncodeNetworkCode(reqOp+6, Bitmap->netcode);
+      reqOp[11] = 8+GSM_GetBitmapSize(Bitmap);
       reqOp[12]=Bitmap->width;
       reqOp[13]=Bitmap->height;
-      reqOp[15]=(Bitmap->width*Bitmap->height+7)/8;
+      reqOp[15]=GSM_GetBitmapSize(Bitmap);
       memcpy(reqOp+count,Bitmap->bitmap,Bitmap->size);
       count += Bitmap->size;
     } else {
@@ -2887,18 +2886,18 @@ GSM_Error N7110_SetRingTone(GSM_Ringtone *ringtone, int *maxlength)
 
   /* Info from Till Toenshoff [till@uni.de]
 
-     One improvement - for looping you can use an alternative header
-     normal
-     0x02, 0xFC, 0x09, 0x00, 0x0A, 0x01
-
-     loop
-     0x02, 0xFC, 0x09, 0x00, 0x05, 0xLL, 0x0A, 0x01
-
-     LL=0x01-0x10
-
-     0x01=loop once
-     [...]
-     0x10=loop infinite
+     One improvement - for looping you can use an alternative header\r
+     normal\r
+     0x02, 0xFC, 0x09, 0x00, 0x0A, 0x01\r
+\r
+     loop\r
+     0x02, 0xFC, 0x09, 0x00, 0x05, 0xLL, 0x0A, 0x01\r
+\r
+     LL=0x01-0x10\r
+\r
+     0x01=loop once\r
+     [...]\r
+     0x10=loop infinite\r
   */
   char tail[] = {0x40, 0x7D, 0x40, 0x5C, 0x0A, 0xFE, 0x40,
                  0x20, 0x40, 0x7D, 0x40, 0x37, 0x0A, 0xFE,
@@ -3060,7 +3059,7 @@ GSM_Error N7110_GetBinRingTone(GSM_BinRingtone *ringtone)
 
 GSM_Error N7110_SetBinRingTone(GSM_BinRingtone *ringtone)
 {
-  unsigned char req[500] = { N6110_FRAME_HEADER, 0x1f, 0x00, 0x00 };
+  unsigned char req[1000] = { N6110_FRAME_HEADER, 0x1f, 0x00, 0x00 };
 
   GSM_NetworkInfo NetworkInfo;
   
@@ -3082,7 +3081,7 @@ GSM_Error N7110_SetBinRingTone(GSM_BinRingtone *ringtone)
 
   memcpy(req+35,ringtone->frame+i,ringtone->length-i);
 
-  Protocol->SendMessage(35+ringtone->length-i, 0x1f, req);  
+  Protocol->SendMessage(35+ringtone->length-i, 0x1f, req);  \r
 
   GSM->GetNetworkInfo(&NetworkInfo); //need to make something    
   
@@ -3191,21 +3190,20 @@ void N7110_Dispatch0x0AMessage(u16 MessageLength, u8 *MessageBuffer, u8 MessageT
                            GSM_GetNetworkName(CurrentGetBitmap->netcode));
 #endif
 
+      CurrentGetBitmap->type=GSM_7110OperatorLogo;
       if (MessageBuffer[4] == 0x02) {   /* logo present */
         count = 7;
         count += MessageBuffer[count];  /* skip network info */
-        CurrentGetBitmap->size=MessageBuffer[count++];   /* is too large */
+        count ++;
         CurrentGetBitmap->width=MessageBuffer[count++];  /* 78 */
         CurrentGetBitmap->height=MessageBuffer[count++]; /* 21 */
         count+=4;
-        CurrentGetBitmap->size=(CurrentGetBitmap->height*CurrentGetBitmap->width+7)/8; /* packed size */
-        if (CurrentGetBitmap->size > sizeof(CurrentGetBitmap->bitmap))
-          CurrentGetBitmap->size=sizeof(CurrentGetBitmap->bitmap);
+        CurrentGetBitmap->size=GSM_GetBitmapSize(CurrentGetBitmap); /* packed size */
         memcpy(CurrentGetBitmap->bitmap,MessageBuffer+count,CurrentGetBitmap->size);
       } else {
         CurrentGetBitmap->width=78;
         CurrentGetBitmap->height=21;
-        CurrentGetBitmap->size=(CurrentGetBitmap->height*CurrentGetBitmap->width+7)/8; /* packed size */
+        CurrentGetBitmap->size=GSM_GetBitmapSize(CurrentGetBitmap); /* packed size */
         memset(CurrentGetBitmap->bitmap,0,CurrentGetBitmap->size);
       }
       CurrentGetBitmapError=GE_NONE;
@@ -3281,7 +3279,7 @@ void N7110_Dispatch0x14Message(u16 MessageLength, u8 *MessageBuffer, u8 MessageT
 
         CurrentGetBitmap->width=MessageBuffer[47];
         CurrentGetBitmap->height=MessageBuffer[48];
-        CurrentGetBitmap->size=CurrentGetBitmap->height*CurrentGetBitmap->width/8;
+        CurrentGetBitmap->size=GSM_GetBitmapSize(CurrentGetBitmap);
       
         memcpy(CurrentGetBitmap->bitmap,MessageBuffer+51,CurrentGetBitmap->size);
 
@@ -3445,8 +3443,10 @@ void N7110_Dispatch0x7AMessage(u16 MessageLength, u8 *MessageBuffer, u8 MessageT
         /* Make sure we are expecting a startup logo */
         if (CurrentGetBitmap && CurrentGetBitmapError == GE_BUSY) {
           CurrentGetBitmap->height=MessageBuffer[13];  /* 96 */
-          CurrentGetBitmap->width=MessageBuffer[17];   /* 60 */
-          CurrentGetBitmap->size=(CurrentGetBitmap->height+7)/8*CurrentGetBitmap->width; /* unpacked size */
+          CurrentGetBitmap->width=MessageBuffer[17];   /* 60/65 */
+          CurrentGetBitmap->type=GSM_7110StartupLogo;
+          if (CurrentGetBitmap->width==60) CurrentGetBitmap->type=GSM_6210StartupLogo;
+          CurrentGetBitmap->size=GSM_GetBitmapSize(CurrentGetBitmap); /* unpacked size */
           if (CurrentGetBitmap->size > sizeof(CurrentGetBitmap->bitmap))
             CurrentGetBitmap->size=sizeof(CurrentGetBitmap->bitmap);
           memcpy(CurrentGetBitmap->bitmap,MessageBuffer+22,CurrentGetBitmap->size);