bladox.com Forum Index bladox.com
Turbo SIM Toolkit Adapter Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Desperately going in circles, pls help!

 
Post new topic   Reply to topic    bladox.com Forum Index -> Development
View previous topic :: View next topic  
Author Message
rhamel
Guest





PostPosted: Wed Sep 13, 2006 12:54 am    Post subject: Desperately going in circles, pls help! Reply with quote

Pavel,
I'm slowly going crazy here. I've gotten to the point where the app actually works. Everything works... for about ten minutes. Then, the barcode scans turn to garbage. It's the same garbage everytime, but definitely garbage. Can you please take a look at my code, and point out what idiotic thing I am doing wrong?

Thanks,
Richard

Code:

 * Copyright (C) 2003 BLADOX, s.r.o.  All rights reserved.
 *
 * This file is part of an example program for Turbo. This example
 * program may be used, distributed and modified without limitation.
 *
 */

#include <config.h>
#include <turbo/turbo.h>

// not sure if these are needed
#include <avr/sfr_defs.h>
#include <avr/io.h>

#include <stdlib.h>
#include <string.h>

#include "uart2i2c.h"
#include "pers_mem.h"

#include <math.h>
#include <ctype.h>

//===================================================================
//
// I moved the localisations to a separate file. It was just too big.
//
//===================================================================

#include "localisation.h"

//==============================================================
//
// number capabilities
// I have no idea what this is. I'm guessing they're attributes
// applied to a phone number record
//
//==============================================================

typedef struct _Number_cap   
{
  u8 *nr;
  u8 alarm;
  u8 alarm_call;
  u8 status;
  u8 call;
  u8 call_confirm;
  u8 sms;
  u8 turbo_sms;
  u8 *fax_sms;         
#ifdef CONFIG_BLOCK
  u8 block;
#endif
#ifdef CONFIG_AIRCON
  u8 aircon;
#endif
} Number_cap;

Pers_mem *pers_mem = NULL;

//=========================================================
//
// in the phone number sub-menu, this points to the number
// currently being affected
//
//=========================================================
SNode *current_number;

//====================================================
//
// nodes in the spider - equivalent to items in a menu
//
//====================================================

SNodeP alarm_n_numbers;
SNodeP alarm_n_new_number;
SNodeP alarm_number_view_n;
SNodeP alarm_number_delete_n;

#ifdef CONFIG_TSMS
SNodeP alarm_number_send_turbo_sms_alarm_n;
#endif

//========================================================
//
// declarations for the functions called by the menu items
//
//========================================================

u8 alarm_number_show   (SCtx * ctx, u8 action);
u8 alarm_numbers       (SCtx * ctx, u8 action);
u8 alarm_new_number    (SCtx * ctx, u8 action);
u8 alarm_number_delete (SCtx * ctx, u8 action);
u8 alarm_number_view   (SCtx * ctx, u8 action);

//===============================================================
//
// this associates the menu item nodes with the functions called
// the 'lc' bits call localised text from localisations.h
// the 'LC' call constants defined elsewhere
//
//===============================================================

SNodeP alarm_n_numbers       = { lc_Numbers,    alarm_numbers };
SNodeP alarm_n_new_number    = { lc_New_Number, alarm_new_number };
SNodeP alarm_number_delete_n = { LC_DELETE,     alarm_number_delete };
SNodeP alarm_number_view_n   = { LC_VIEW,       alarm_number_view };

//======================================================
//
// declare an array of links between the menu item nodes
//
//======================================================

SEdgeP alarm_edges_p[];

//=================================================
//
// set up the links to point to the menu item nodes
//
//=================================================

SEdgeP alarm_edges_p[] = {
  {&alarm_n_numbers,          &alarm_n_new_number},
  NULL
};

//==============================================================================
//
// this is the VIEW option after a phone number has been selected from the list
// of phone numbers. Currently, selecting this option gives the error message:
// "Not enough RAM memory". It doesn't seem to hurt anything though.
//
//==============================================================================

u8 alarm_number_view (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    //======================
    //
    // what does buf_B() do?
    //
    //======================

    u8 *buf        = buf_B ();
    u8 *r          = buf;

    //============================================
    //
    // ok, where's he getting current_number from?
    //
    //============================================

    SNode *np      = current_number;
    Number_cap *nc = rw (&np->p);

    //=====================================
    //
    // copy a phone number from nc->nr to s
    //
    //=====================================

    u8 *s          = msisdn2str (rw (&nc->nr), MSISDN_ADN, MEM_R);
    u8 i;

    if (s)
    {
      //==================
      //
       // copy from s to r?
      //
      //==================
      
      r = sprints (r, s);

      //=============
      //
      // stringify r?
      //
      //=============

      r = sprintc (r, '\0');
      free (s);

      //==============================
      //
      // display the string now in buf
      //
      //==============================

      i = display_text (buf, NULL);
      if (i != APP_END)
         return APP_BACK;
      return i;
    }
    else
      return APP_BACK;
  }
  return APP_OK;
}


//===============================================================================
//
// this is the DELETE option after a phone number has been selected from the list
// of phone numbers.
//
//===============================================================================

u8 alarm_number_delete (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    u8 res;

   //======================
   //
   // confirm with the user
   //
   //======================

    res = yesno (NULL, 1, 1);
    if (res == APP_END)
      return APP_END;

   //==================
   //
   // if ok to go ahead
   //
   //==================

    if (res == APP_YES)
    {
      // DELETE
      SEdge *e, *e1;
      SNode *n = ctx->parent->f;
      Number_cap *nc;

      e1 = e = rw (&pers_mem->numbers);
      while (e && rw (&e->t) != n)
      {
         e1 = e;
         e = rw (&e->next);
      }
      if (e)
      {
         if (e == e1)
              ww (&pers_mem->numbers, rw (&e->next));
         else
              ww (&e1->next, rw (&e->next));

         efree (e);
         nc = rw (&n->p);
         efree (rw (&nc->nr));
         efree (rw (&nc->fax_sms));
         efree (nc);
         efree (rw (&n->text));
         efree (n);
      }
      spider_back (ctx);
    }
    return APP_BACK;
  }
  return APP_OK;
}

//====================================
//
// I'm guessing this adds a new number
//
//====================================

u8 alarm_new_number (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    u8         *res;
    u8         *t;
    u8         *nr;
    SNode      *n;
    SEdge      *e, *e1;
    u8         j, i;
    u8         alpha[ALPHA_LEN];
    Number_cap *nc;

    res = msisdn (alpha);
    if (res == ENULL)
      goto X1;
    if (res == NULL)
      goto X2;
    n = emalloc (sizeof (SNode));
    if (n == NULL)
      goto X3;
    e = emalloc (sizeof (SEdge));
    if (n == NULL)
      goto X4;
    j = strlen (alpha);
    t = emalloc (j + 1);
    if (t == NULL)
      goto X5;
    nr = msisdncpy (res, MSISDN_ADN, MEM_E);
    if (nr == NULL)
      goto X6;
    memcpy (t, alpha, j);
    wb (t + j, '\0');
    nc = emalloc (sizeof (Number_cap));
    if (nc == NULL)
      goto X7;
    ww (&nc->nr,            nr);
    wb (&nc->alarm,         1);
    wb (&nc->alarm_call,    0);
    wb (&nc->status,        1);
    wb (&nc->sms,           1);
    wb (&nc->call,          1);
    wb (&nc->call_confirm,  0);
    wb (&nc->turbo_sms,     0);
    ww (&nc->fax_sms,       0);

#ifdef CONFIG_BLOCK
    wb (&nc->block,         1);
#endif
#ifdef CONFIG_AIRCON
    wb (&nc->aircon,        1);
#endif

    ww (&n->cb,             alarm_number_show);
    ww (&n->text,           t);
    ww (&n->p,              nc);

    ww (&e->next,           rw(&pers_mem->numbers));
    ww (&e->t,              n);
    ww (&e->f,              &alarm_n_numbers);
    wb (&e->source,         MEM_F_P | MEM_T_E);
    ww (&pers_mem->numbers, e);
    goto X2;
  X7:
    efree (nr);
  X6:
    efree (t);
  X5:
    efree (e);
  X4:
    efree (n);
  X3:
    perror (ERR_NO_EEPROM);
  X2:
    return APP_BACK;
  X1:
    return APP_END;
  }
  return APP_OK;
}

//======================================================================
//
// when a phone number has been selected from the list of phone numbers
// this displays the VIEW & DELETE options for that number
// essentially, the number gets inserted into the spider as a menu item
// very clever really
//
//======================================================================

u8 alarm_number_show (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    current_number = ctx->n;
#ifdef CONFIG_TSMS
    spider_append_r (ctx, &alarm_number_send_turbo_sms_alarm_n);
#endif
    spider_append_r (ctx, &alarm_number_delete_n);
    spider_append_r (ctx, &alarm_number_view_n);
  }
  else if (action == APP_LEAVE)
  {
    spider_clear_r (ctx);
  }
  return APP_OK;
}

//======================================================================
//
// not sure exactly how it's done, but I think this has to do with
// inserting the phone numbers into the menu as menu items
//
//======================================================================

u8 alarm_numbers (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    ctx->eE = rw (&pers_mem->numbers);
  }
  else if (action == APP_LEAVE)
  {
    ctx->eE = NULL;
  }
  return APP_OK;
}

//==============================================================================
//==============================================================================
//==============================================================================
//
//                           1         2         3         4         5         6
//                  123456789012345678901234567890123456789012345678901234567890
//
//==============================================================================
//==============================================================================
//==============================================================================

u8 PROGMEM msg[]      = "BARCODE GOES HERE"; // SMS Msg
u8 PROGMEM phonenum[] = "4937749" ;          //  phone number

//======================================================
//
// my_status is a flag that directs flow control somehow
//
//======================================================

u8 my_status;

//======================================================
//
// poll_int is used to change the polling interval of
// the phone. This allows me to scan more frequently
// than the defaut frequency
//
//===================================================

u8 poll_int;
b8 my_poll_interval (u8 d);

//===============================================
//
// secret parameter X that makes send_sms() work
//
//===============================================

#define PARAMX 0x12

//======================================================
//
// secret address where the slave microcontroller lives?
//
//======================================================

#define MY_SLAVE_ADDRESS 0x7E

void action_sms (void *data)
{

//=============================
//
// allocate space for SMS text
//
//=============================

  u8 *tmp = malloc (100);
  u8 i;
  u8 barcode[7];
  u16 nr_chars;
  u8 *fone;

  SEdge *nrs = rw(&pers_mem->numbers);
  SNode *n;
  Number_cap *nc;

start_over:
   barcode[0] = '\0';
   barcode[1] = '\0';
   barcode[2] = '\0';
   barcode[3] = '\0';
   barcode[4] = '\0';
   barcode[5] = '\0';
   barcode[6] = '\0';

   uart2i2c_read_rxchars(&nr_chars);

   if (nr_chars != 0)
   {
        uart2i2c_read_rxfifo_data (barcode, nr_chars);
      barcode[nr_chars] = '\0';
   }
   else
      return;  // don't send empty scans (??)

// this results in "strlen(barcode) = 7"
/*
   u8 *res;
   u8 *t = buf_B();
   res = t;

   res = sprints(res, "strlen(barcode) = ");
   res = sprinti(res, (u32) strlen(barcode));
   res = sprintc(res, '\0');
   display_text_raw(t,   Q_DISPLAY_TEXT_USER_CLEAR);
*/

if (strlen(barcode) == 0)
   return;
      
   display_text_raw(barcode, Q_DISPLAY_TEXT_USER_CLEAR);

//=========================================
//
// copy the SMS message to the text buffer
//
//=========================================

  memcpy (tmp, barcode, nr_chars);

  display_text_raw(tmp, Q_DISPLAY_TEXT_USER_CLEAR);

//===========================
//
// change message encoding??
//
//===========================

  /*
  if (dcs_i_7b(tmp[0]))
     dcs_78 (tmp, nr_chars, DCS_8_TO_7);
  else
     dcs_78 (tmp, nr_chars, DCS_7_TO_8);
   */

//======================
//
// send the SMS message
//
//======================

while (nrs != NULL)
{
   n = rw(&nrs->t);
   nc = rw(&n->p);
   fone = msisdncpy(rw(&nc->nr), MSISDN_ADN, MEM_R);
   if (fone == NULL)
      return;
   send_sms (tmp, nr_chars, fone, MSISDN_ADN, PARAMX, 0, NULL, NULL);
     nrs = rw(&nrs->next);
}

//==========
//
// clean up
//
//==========

  free (tmp);
  free (fone);
  free (barcode);
  free (nr_chars);
}

b8 my_poll_interval (u8 d)
{
  u8 *res;
  u8 j;

//=================================================
//
// do all functions beginning with 'db' output
// to standard error when using the edge connector
// debugging device?
//
//=================================================

  dbsp ("MY_POLL_INT\n");

//===============================
// initial value of poll interval
//===============================

  poll_int = 30;

  //=====================================================
  //
  // but we're setting it to DURATION_SEC which means ??
  // or to d, which is supposed to be 4 seconds
  //
  //=====================================================
 
  res = poll_interval (DURATION_SEC, d);

  j = get_tag (res, T_RESULT);
  if (j == 0)
    return -1;
  j++;            // tag
  j++;            // len
 
  if (res[j] != 0x00)
  {
    dbsp ("SET POLL INTERVAL ERROR\n");
    return -1;
  }

  j = get_tag (res, T_DURATION);
  if (j == 0)
    return -1;
  j++;            // tag
  j++;            // len
  dbsp ("POLL INT UNITS ");
  dbch (res[j]);
  dbc ('\n');
  j++;            // units
  dbsp ("POLL INT ");
  dbch (res[j]);
  dbc ('\n');
  poll_int = res[j];
  return res[j];
}

void action_menu (Menu_selection_data *data)
{
   //===============================================
   //
   // I believe this sets up the SETUP sub-menu here
   //
   //===============================================

   SCtx *c;
   c = spider_init();
   spider_set_order(c, ORDER_R, ORDER_P, ORDER_E, ORDER_D);
   c->eP = &alarm_edges_p;

   if (data->item == 0) // Scan barcode here
   {
      //============================
      //
      // set poll interval to 5 secs
      //
      //============================

      my_poll_interval (5);
      reg_action(ACTION_STATUS);
   }
   else // set up phone numbers here
   {
      c->n = &alarm_n_numbers;
      spider(c);
   }
}

void turbo_handler (u8 action, void *data)
{
  switch (action)
  {
    case ACTION_APP_REGISTER:
    {
      Pers_mem *p = emalloc (sizeof (Pers_mem));
      if (p)
      {
           u8 i;

         //=============================
         //
         // setup PIN to '1111', I think
         //
         //=============================

           for (i = 0; i < 4; i++)
             wb (&p->pin[i], '1' + i);
            
         //===============================
         //
         // set up default values for app?
         //
         //===============================

           ww (&p->numbers,            NULL);
           wb (&p->sens,               DEF_SENS);
           wb (&p->baudrate,           DEF_BAUDRATE);
           wb (&p->manual_delay_on,    DEF_MANUAL_DELAY_ON);
           wb (&p->manual_delay_off,   DEF_MANUAL_DELAY_OFF);
           wb (&p->repeated_sms_delay, 0);
           ww (&p->net_reset,          DEF_NET_RESET);
           wb (&p->alarm_1,            0);
           wb (&p->alarm_2,            0);
           wb (&p->central_lock,       0);
           wb (&p->is_pin,             0);
           wb (&p->poll_interval,      DEF_POLL_INTERVAL);
           wb (&p->interval_nmea,      1);          
           wb (&p->status_spawn_bug,   0);

           reg_app_data (p);
      }

      //=======================
      //
      // no idea what this does
      //
      //=======================

      set_proc_8 (PROC_8_CONFIG_SETUP,         0);
      set_proc_8 (PROC_8_CONFIG_BINARY_CHOICE, 0);
      set_proc_8 (PROC_8_CONFIG_SIM_FIRST,     0);
    }
    break;

    case ACTION_APP_INIT:
      //===========================
      //
       // what does this do, really?
      //
      //===========================

       pers_mem = app_data(); // init app's data??
      init_uart2i2c (MY_SLAVE_ADDRESS);

      //===================================
      //
      // set baud rate to 9600
      //
      //===================================

      uart2i2c_write_ubrr(51);
      break;

    case ACTION_INSERT_MENU:
      insert_menu ("SCAN BARCODE");
      insert_menu ("SETUP");
      break;

    case ACTION_MENU_SELECTION:
      //===============================================================
      //
      // when user selects this app, display the little two-option menu
      //
      //===============================================================

      stk_thread (action_menu, data);
      break;

    case ACTION_STATUS:
      //=====================================================
      //
      // after user presses SCAN BARCODE, it should come here
      //
      //=====================================================
      if (my_status == 0)
         stk_thread(action_sms, NULL);
      break;

    default:
      break;
  }
}
//=====================================================
//=====================================================
//=====================================================
//
// End of code
//
//=====================================================
//=====================================================
//=====================================================

[/code]
Back to top
pz
Guest





PostPosted: Wed Sep 13, 2006 6:35 am    Post subject: Re: Desperately going in circles, pls help! Reply with quote

rhamel wrote:
Pavel,
I'm slowly going crazy here. I've gotten to the point where the app actually works.


That's great!

Quote:

Everything works... for about ten minutes. Then, the barcode scans turn to garbage. It's


My guess is something memory related, I see one candidate.

Btw. nice code comments.

Quote:

the same garbage everytime, but definitely garbage. Can you please take a look at my code, and point out what idiotic thing I am doing wrong?

Thanks,
Richard

Code:

 
//=============================
//
// allocate space for SMS text
//
//=============================

  u8 *tmp = malloc (100);
  u8 i;
  u8 barcode[7];
  u16 nr_chars;
  u8 *fone;

.....


//==========
//
// clean up
//
//==========

  free (tmp);
  free (fone);
  free (barcode);
  free (nr_chars);
}




You are freeing barcode, nr_chars which are on stack, not heap. Don't do this. Also fone should be free'd in the cycle you are using it because always new one is malloced and not free'd. This cleans only the last one - and if none then something random because it's not initiated to NULL.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    bladox.com Forum Index -> Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group