⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplesms.c

📁 palm编程.简单的基于palm os smart phone上开发的sms程序.
💻 C
📖 第 1 页 / 共 2 页
字号:
/** 
 * \file   SimpleSMS.c
 *
 * SimpleSMS
 *
 * This is the main source file for the Simple SMS App. This
 * code demonstrates how to write your own SMS application. The app
 * will register with the Phone Library, send a custom message, and
 * display a dialog for incoming messages.
 *
 * \license
 * 
 * Copyright (c) 2002 Handspring Inc., All Rights Reserved
 *
 * $Id:$
 *
 ***************************************************************/
#include <PalmOS.h>
#include <HsNav.h>
#include <HsExt.h>
#include <HsPhone.h>

#include "SimpleSMS.h"

// -------------------------------------------------------------
// Constants
// -------------------------------------------------------------
#define appFileCreator			   'HSMS'
#define appVersionNum              0x01
#define appPrefID                  0x00
#define appPrefVersionNum          0x01
#define kMinVersionSupported		0x3523040
// -------------------------------------------------------------
// Structures
// -------------------------------------------------------------
#define serviceCenterSize		40
#define addressSize				40
#define messageSize				160

typedef struct 
{
	char address[addressSize + 1];
	char message[messageSize + 1];
} SimpleSMSPreferenceType;

typedef struct NBSNotificationEventType PhnNBSNotificationEventType;
// -------------------------------------------------------------
// Globals
// -------------------------------------------------------------
static SimpleSMSPreferenceType smsPrefs;
static Boolean smsLibLoaded;
static UInt16 smsLibRef;

// -------------------------------------------------------------
// Prototypes
// -------------------------------------------------------------
static Err		StartApplication (void);
static void		EventLoop (void);
static void		StopApplication (void);
static Boolean	SimpleSMSEvent (EventPtr event);

static UInt32	HandlePhoneLibraryEvent (MemPtr cmdPBP, UInt16 launchFlags);
static Err		LoadPhoneLibrary (UInt16* libRefP, Boolean* libLoadedP);
static Err		SMSRegister (Boolean bReg);
static void		SendTheMessage (void);
static void		DisplayMessage (UInt32 messageID);
static void		TurnOnOptionLock (void);
static void*	GetObjectPtr (UInt16 objectID);
static void		CopyFromGlobalToFields (void);
static UInt32		CopyFromFieldsToGlobal (void);

static void     HandleNotificationEvent ( MemPtr cmdPBP, UInt16 launchFlags);

// -------------------------------------------------------------
// Functions
// -------------------------------------------------------------

/****************************************************************/
/* PilotMain */
/**
 * Main entry point
 *
 * \param   cmd			  IN    Value specifying the launch code. 
 * \param   cmdPBP		  IN	Pointer to a structure that is associated with the launch code. 
 * \param	launchFlags	  IN	Value providing extra information about the launch.
 *
 * \retval  UInt32
 *
 ****************************************************************/
UInt32
PilotMain (UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags)
{
  UInt32 error;

  switch (cmd)
	{
	  case sysAppLaunchCmdNormalLaunch:
		// normal application launch
		error = StartApplication ();
		if (error) 
		  return error;

		EventLoop ();

		StopApplication ();
		break;

	  case phnLibLaunchCmdRegister:   
	  	// Received when device is reset (or the library is loaded ?)
	  	// DbgBreak();
		// SMSRegister (true);
		break;

	  case 0x7000:	// For Treo 300 workaround. Currently the launch code is different on Treo 300 than 180/270
	  case phnLibLaunchCmdEvent:
		// we have an event from the Phone library, so handle it
		error = HandlePhoneLibraryEvent (cmdPBP, launchFlags);
		if (error) 
		  return error;
		break;

	  case sysAppLaunchCmdNotify:
		HandleNotificationEvent (cmdPBP, launchFlags);
		break; 
	  

	  default:
		break;
	}

  return errNone;
}

/****************************************************************/
/* StartApplication */
/**
 * Application start 
 *
 * \retval  Err
 *
 ****************************************************************/
static Err
StartApplication (void)
{
	Err		error = 0;
	char 	errMsg[10];

	smsLibLoaded = false;
	smsLibRef = 0;


#if 0  // This block is not really neccessary since we use the Handspring/Treo's phone libraries
	   // whick guarantees the device is (should be) a Treo.

	UInt16	prefsSize;
	void*	procP;
	UInt32	value;

	// Make sure that we are running on the Treo
	// Make sure that we are at the appropriate Palm OS version
	if (!FtrGet (hsFtrCreator, hsFtrIDVersion, &value))
	{
		if (value < kMinVersionSupported)
		{
			FrmCustomAlert (resAlertPhoneLibrary, "PalmOS version too low", errMsg, NULL);
			return 1;
		}
	}
	else
	{
		FrmCustomAlert (resAlertPhoneLibrary, "No hsFtrIDVersion", errMsg, NULL);
		return 1;
	}

  // Make sure that we support the HsIndicator function call.
  // We use this test to make sure that we are on a Treo device.
  procP = HsGetTrapAddress (hsSelIndicator);
  if (procP && procP != HsGetTrapAddress(hsSelUnimplemented))
	{
	  // Read the saved preferences / saved-state information.
	  prefsSize = sizeof(SimpleSMSPreferenceType);
	  if (PrefGetAppPreferences (appFileCreator, appPrefID, &smsPrefs, &prefsSize, true) != noPreferenceFound)
	  	;
	}
  else
	{
	  // Display the error dialog and return with error
	FrmCustomAlert (resAlertPhoneLibrary, "No hsSelIndicator proc", errMsg, NULL);
	  return 1;
	}

#endif

  
  // load the Phone Library
	error = LoadPhoneLibrary (&smsLibRef, &smsLibLoaded);
	if (error)
	{
		StrIToA(errMsg, error);
		FrmCustomAlert (resAlertPhoneLibrary, "Can't Load the Phone Library", errMsg, NULL);
		return 1;
	}
	else
	{	// We were able to load the library
		// Now, let's register with the SMS library
		error = SMSRegister (true);
		if (error)
			FrmAlert (resAlertCantRegisterWithSMSLibrary);
	}

  // verify the Phone is powered and SIM status is ready
  if ( PhnLibModulePowered (smsLibRef) == false)
  	{
	  FrmAlert (resAlertPhoneNotReady);
	  if (smsLibLoaded)
	  	(void)PhnLibClose (smsLibRef);
	  return 1;
	}

  // Check what device we are on: GSM or CDMA
  
  FrmGotoForm(resFormIDSimpleSMS);
  
  /*
  (void)HsAttrGet (hsAttrPhoneType, 0, &phoneType);
  if (phoneType == hsAttrPhoneTypeGSM)
	{
	  FrmGotoForm (resFormIDSimpleSMS);
	}
  else if (phoneType == hsAttrPhoneTypeCDMA)
	{
	  FrmGotoForm (resFormIDSimpleSMSNoSend);
	}
  else
	{
	  // Default to the GSM (send/receive message) form. Pre-Treo 300 devices do not support this attribute.
	  FrmGotoForm (resFormIDSimpleSMS);
	  phoneType = hsAttrPhoneTypeGSM;
	}
  */
  
  return error;
}

/****************************************************************/
/* EventLoop */
/**
 * Main event loop
 *
 * \retval  none
 *
 ****************************************************************/
static void EventLoop(void)
{
  UInt16	err;
  UInt16	formID;
  FormPtr	formP;
  EventType event;

  do
	{
	  EvtGetEvent (&event, sysTicksPerSecond/2);

	  if (SysHandleEvent (&event))
		continue;
	  if (MenuHandleEvent ((void *)0, &event, &err))
		continue;
	  if (event.eType == frmLoadEvent)
		{
		  formID = event.data.frmLoad.formID;
		  formP = FrmInitForm (formID);
		  FrmSetActiveForm (formP);

		  switch (formID) 
			{
			case resFormIDSimpleSMS:
			  FrmSetEventHandler (formP, (FormEventHandlerPtr)SimpleSMSEvent);
			  break;
			case resFormIDSimpleSMSNoSend:
			  FrmSetEventHandler (formP, (FormEventHandlerPtr)SimpleSMSEvent);
			  break;
			}
		}

	  FrmDispatchEvent(&event);

	 } while (event.eType != appStopEvent);
}

/****************************************************************/
/* StopApplication */
/**
 * Stop the application
 *
 * \retval  none
 *
 ****************************************************************/
static void
StopApplication (void)
{
  //if (phoneType == hsAttrPhoneTypeGSM)
	//{
	//  CopyFromFieldsToGlobal();
	//}

  // Write the saved preferences / saved-state information.  This data 
  // will be backed up during a HotSync.
  //PrefSetAppPreferences (appFileCreator, appPrefID, appPrefVersionNum, &smsPrefs, sizeof (smsPrefs), true);

  // Close all the open forms.
  FrmCloseAllForms ();

  // Release the SMS library
  if (smsLibLoaded)
	  PhnLibClose(smsLibRef);
}

/****************************************************************/
/* SimpleSMSEvent */
/**
 * Event handler for the main form
 *
 * \param	event		  IN	Event to process
 *
 * \retval  Boolean
 *
 ****************************************************************/
static Boolean
SimpleSMSEvent (EventPtr eventP)
{
  UInt16 err;
  UInt16  cardNo;
  FormPtr frmP;
  LocalID dbID;
  FieldPtr fieldP;
  DmSearchStateType searchState;
  Boolean handled = false;

  DmGetNextDatabaseByTypeCreator(true, &searchState, sysFileTApplication,
							   appFileCreator, true, &cardNo, &dbID);

  switch (eventP->eType) 
	{
	case menuEvent:
	  switch (eventP->data.menu.itemID)
		{
		case resMenuItemAbout:
		  MenuEraseStatus (0);
		  HsAboutHandspringApp (cardNo, dbID, "2007", "palmOne DTS Team");
		  handled = true;
		  break;

		case resMenuItemUnRegister:
		  MenuEraseStatus (0);
		  err = SMSRegister (false);
		  if (err)
			FrmAlert (resAlertCantRegisterWithSMSLibrary);
		  handled = true;
		  break;
		}
	  break;

	case frmOpenEvent:
	  frmP = FrmGetActiveForm();

		  // Set the focus to the edit field
	  FrmSetFocus(frmP, noFocus);
	  FrmSetFocus(frmP, FrmGetObjectIndex(frmP, resButtonSend));
	  FrmDrawForm (frmP);

	  if (FrmGetActiveFormID () == resFormIDSimpleSMS)
		{
		  CopyFromGlobalToFields();



		  TurnOnOptionLock ();
		}
	  else if (FrmGetActiveFormID () == resFormIDSimpleSMSNoSend)
		{
		}

	  // Setup the battery and signal gadgets
	  {
		UInt32 hsStatusVersion;

		if (FtrGet (hsFtrCreator, hsFtrIDStatusGadgetRev, &hsStatusVersion) == 0)
		  {
			HsStatusSetGadgetType (frmP, resGadgetMainBatteryLevel, hsStatusGadgetBattery);
			HsStatusSetGadgetType (frmP, resGadgetSignalLevel, hsStatusGadgetSignal);
		  }
	  }

	  handled = true;
	  break;
		
	case ctlSelectEvent:
	  switch (eventP->data.ctlSelect.controlID)
		{
		case resButtonSend:
		  SendTheMessage();
		  break;
			
		default:
		  break;
		}
	  break;

	case fldEnterEvent:
	  fieldP = (FieldType*)GetObjectPtr (eventP->data.fldEnter.fieldID);
	  switch (eventP->data.fldEnter.fieldID)
		{
		case resFieldAddress:
		case resFieldServiceCenter:
          FldHandleEvent (fieldP, eventP);
		  TurnOnOptionLock ();
          handled = true;
		  break;

		case resFieldMessage:
          FldHandleEvent (fieldP, eventP);
          if (FldGetInsPtPosition(fieldP) == 0)
            HsGrfSetStateExt(false, false, false, true, false, true);
          handled = true;
		  break;
		}
	  break;

	default:
	  break;
	}
	
	return handled;
}




/****************************************************************/
/* HandleNotificationEvent */
/**
 * This function handles all Phone specific events.
 *
 * \param   cmdPBP		  IN	Pointer to a structure that is associated with the launch code. 
 * \param	launchFlags	  IN	Value providing extra information about the launch.
 * 
 * \retval	UInt32 - Result of launch
 *
 ****************************************************************/
static void 
HandleNotificationEvent ( MemPtr cmdPBP, UInt16 launchFlags) 
{
  SysNotifyParamType* notifyParam = (SysNotifyParamType*)cmdPBP ;

  launchFlags = launchFlags;		// Prevent compiler error
  
  if (notifyParam->notifyType == phnNBSEvent)
    {
      PhnNBSNotificationEventType* NBSParamP = (PhnNBSNotificationEventType*) notifyParam->notifyDetailsP;


      // Display NBS info 

      if (NBSParamP->binary)	// NBS binary
	  {
	  	;
	  }
	  else // NBS text
	  {

		char dstPort[20], srcPort[20];

//		  NBSParamP->refnum;
//		  NBSParamP->maxnum;
//		  NBSParamP->seqnum;

//		  NBSParamP->srcPort;
//		  NBSParamP->dstPort;
	
//		  NBSParamP->senderP;
//		  NBSParamP->datetime;

		StrIToA(dstPort, (long) NBSParamP->dstPort);
		StrIToA(srcPort, (long) NBSParamP->srcPort);
		StrCat(dstPort, "/");
		StrCat(dstPort, srcPort);
		FrmCustomAlert(resAlertIncomingNBSMessage, NBSParamP->senderP, dstPort, NBSParamP->dataP);


		//Display like regular SMS.
		//DisplayMessage(NBSParamP->msgID);

	  }
      
   
	  notifyParam->handled = true;	

    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -