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

📄 mbus.c

📁 Nokia手机M2Bus通讯协议底层程序.对手机编程开发人员有用
💻 C
📖 第 1 页 / 共 3 页
字号:
/********************************************************************
*                                                                   *
* FILE NAME:                                                        *
*                                                                   *
* PURPOSE:                                                          *
*                                                                   *
* FILE REFERENCES:                                                  *
*                                                                   *
* Name:                                                             *
*                                                                   *
* I/O:                                                              *
*                                                                   *
* Description:                                                      *
*                                                                   *
* EXTERNAL VARIABLES:                                               *
*                                                                   *
* Source:  <      >                                                 *
*                                                                   *
* Name:                                                             *
*                                                                   *
* Type:                                                             *
*                                                                   *
* I/O:                                                              *
*                                                                   *
* Description:                                                      *
*                                                                   *
*                                                                   *
* EXTERNAL REFERENCES:                                              *
*                                                                   *
* Name:                                                             *
*                                                                   *
* Description:                                                      *
*                                                                   *
*                                                                   *
* ABNORMAL TERMINATION CONDITIONS, ERROR AND WARNING MESSAGES:      *
*                                                                   *
* ASSUMPTIONS, CONSTRAINTS, RESTRICTIONS:                           *
*                                                                   *
* NOTES:                                                            *
*                                                                   *
* REQUIREMENTS/FUNCTIONAL SPECIFICATIONS REFERENCES:                *
*                                                                   *
* DEVELOPMENT HISTORY:                                              *
*                                                                   *
* Date:                                                             *
*                                                                   *
* Author:                                                           *
*                                                                   *
* Change Id:                                                        *
*                                                                   *
* Release:                                                          *
*                                                                   *
* Description of change:                                            *
*                                                                   *
********************************************************************/
#include "../include/config.h"

#include <Windows.h>
#include <stdio.h>

#include "../include/sysdef.h"
#include "../include/fls1pub.h"
#include "miscpub.h"
#include "mbusloc.h"
#include "mbuspub.h"

extern eFLASH_ERROR flash_errno;

static UINT8 AU8readbuffer[4096];
static UINT8 AU8transferpacket[256];

static UINT32 MBUSdcb[7] = {
  sizeof(DCB),
  9600,
  0x00001403,
  0x01000000,
  0x01080100,
  0x3F131100,
  0x0000001A
};

static tMBUS_CONFIG mbus_config;
static HANDLE hComm = NULL;

const tMBUSCMD MBUS_getversioncmd = {"Get version", 0x40, "\x00\x01\xC8\x10", 4, 11};
const tMBUSCMD MBUS_getproductcodecmd = {"Get product code", 0x40, "\x00\x01\xCa\x01", 4, 11};
const tMBUSCMD MBUS_enableextendedcmdscmd = {"Enable extended commands", 0x40, "\x00\x01\x64\x01", 4, 11};
const tMBUSCMD MBUS_resetphonesetcmd = {"Reset phone settings", 0x40, "\x00\x01\x65\x01", 4, 11};
const tMBUSCMD MBUS_resetuifsetcmd = {"Reset UIF settings", 0x40, "\x00\x01\x65\x08", 4, 11};
const tMBUSCMD MBUS_resetallsetcmd = {"Reset all (UIF,SCM) settings", 0x40, "\x00\x01\x65\x38", 4, 11};
const tMBUSCMD MBUS_resetdevsetcmd = {"Reset DEV settings", 0x40, "\x00\x01\x65\x02", 4, 11};
const tMBUSCMD MBUS_resetfactorysetcmd = {"Reset full factory settings", 0x40, "\x00\x01\x65\xFF", 4, 11};
const tMBUSCMD MBUS_enableservicemodecmd = {"Enable service mode", 0x40, "\x00\x01\x64\x02", 4, 11};
const tMBUSCMD MBUS_resetphonecmd = {"Reset phone", 0x40, "\x00\x01\x64\x03", 4, 11};
const tMBUSCMD MBUS_getIMEIcmd = {"Get IMEI", 0x40, "\x00\x01\x66", 3, 10};
tMBUSCMD MBUS_getMSIDcmd = {"Get MSID", 0x40, "\x00\x01" MBUS_MSID_CMD_STR "\x00\x00", 5, 11};
const tMBUSCMD MBUS_getMCUchecksum = {"Get MCU checksum", 0x40, "\x00\x01\xc8\x02", 4, 11};
const tMBUSCMD MBUS_getoriginalIMEIcmd = {"Get original IMEI", 0x40, "\x00\x01\xCC\x01", 4, 11};
const tMBUSCMD MBUS_getASICcmd = {"Get ASIC", 0x40, "\x00\x01\xC8\x0C", 4, 11};
const tMBUSCMD MBUS_getCOBBAcmd = {"Get COBBA", 0x40, "\x00\x01\xC8\x0D", 4, 11};
const tMBUSCMD MBUS_getinternalDSPcmd = {"Get internal DSP sw", 0x40, "\x00\x01\xC8\x09", 4, 11};
const tMBUSCMD MBUS_getexternalDSPcmd = {"Get external DSP sw", 0x40, "\x00\x01\xC8\x03", 4, 11};
const tMBUSCMD MBUS_getlanguagepackcmd = {"Get language pack", 0x40, "\x00\x01\xC8\x12", 4, 11};
const tMBUSCMD MBUS_getsecuritycodecmd = {"Get security code", 0x40, "\x00\x01\x6e\x01", 4, 11};
const tMBUSCMD MBUS_getsimlockinfocmd = {"Get simlock info", 0x40, "\x00\x01\x8a\x00", 4, 11};
const tMBUSCMD MBUS_getordernumbercmd = {"Get order number", 0x40, "\x00\x01\xca\x02", 4, 11};
const tMBUSCMD MBUS_getproductsncmd = {"Get product sn", 0x40, "\x00\x01\xca\x03", 4, 11};
const tMBUSCMD MBUS_getbasicproductcodecmd = {"Get basic product code", 0x40, "\x00\x01\xca\x04", 4, 11};
const tMBUSCMD MBUS_getmanufacturemonthcmd = {"Get manufacture month", 0x40, "\x00\x01\xcc\x02", 4, 11};
const tMBUSCMD MBUS_getpurchasedatecmd = {"Get purchase date", 0x40, "\x00\x01\xcc\x04", 4, 11};


/********************************************************
*                                                       *
* FUNCTION NAME:                                        *
*                                                       *
* ARGUMENTS:                                            *
*                                                       *
* ARGUMENT NAME:                                        *
*                                                       *
* TYPE:                                                 *
*                                                       *
* I/O:                                                  *
*                                                       *
* DESCRIPTION                                           *
*                                                       *
*                                                       *
* RETURNS:                                              *
*                                                       *
*********************************************************/

BOOL MBUS_OpenCOMPort(UINT8 U8comport, UINT32 U32comdelay)
{
  char *COMport = "COMx";

  mbus_config.Binitialized = FALSE;

  MDBGSTR2("MBUS_OpenCOMPort : opening COM%d with delay %d",U8comport,U32comdelay);

  if (U8comport<1 || U8comport>4)
  {
    flash_errno = FE_INVALID_COM_PORT;
    return FALSE;
  }

  if (hComm) /* opened? */
  {
    CloseHandle(hComm);
  }

  COMport[3] = U8comport+'0';
  hComm = CreateFile(COMport, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
                     FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );

  if (hComm == NULL)
  {
    flash_errno = FE_INVALID_COM_PORT;
    return FALSE;
  }

  if (!SetCommState(hComm, (DCB *)&MBUSdcb)) {
    CloseHandle(hComm);
    flash_errno = FE_INVALID_COM_PORT;
    return FALSE;
  }

  GetCommTimeouts(hComm, &mbus_config.newcommtimeouts);
  mbus_config.oldcommtimeouts  = mbus_config.newcommtimeouts;
  mbus_config.newcommtimeouts.ReadTotalTimeoutConstant = mbus_config.U16comdelay = U32comdelay;
  SetCommTimeouts(hComm, &mbus_config.newcommtimeouts);

  mbus_config.U8sequencenumber = 0x00;
  mbus_config.Binitialized = TRUE;
  return TRUE;
}


/********************************************************
*                                                       *
* FUNCTION NAME:                                        *
*                                                       *
* ARGUMENTS:                                            *
*                                                       *
* ARGUMENT NAME:                                        *
*                                                       *
* TYPE:                                                 *
*                                                       *
* I/O:                                                  *
*                                                       *
* DESCRIPTION                                           *
*                                                       *
*                                                       *
* RETURNS:                                              *
*                                                       *
*********************************************************/

void MBUS_CloseCOMPort(void)
{
  if (mbus_config.Binitialized) 
  {
    HDBGSTR("MBUS_CloseCOMPort : Closing COM port");
    SetCommTimeouts(hComm, &mbus_config.oldcommtimeouts);
    CloseHandle(hComm);
  }
}


/********************************************************
*                                                       *
* FUNCTION NAME:                                        *
*                                                       *
* ARGUMENTS:                                            *
*                                                       *
* ARGUMENT NAME:                                        *
*                                                       *
* TYPE:                                                 *
*                                                       *
* I/O:                                                  *
*                                                       *
* DESCRIPTION                                           *
*                                                       *
*                                                       *
* RETURNS:                                              *
*                                                       *
*********************************************************/

static BOOL MBUS_checkreply(UINT8 *pU8packet, UINT8 *pU8reply, UINT32 U32replylen)
{
    UINT16 U16packetsize, U16replysize;
    SINT16 count;
    UINT8 U8replychk;
    UINT8 U8replylen;

    if (pU8packet[0]!=pU8reply[0])
        return FALSE;

    U16packetsize = misc_swapword(*(UINT16 *)(pU8packet+1));
    U16replysize = *(UINT16 *)(pU8reply+1);
    if (U16packetsize!=U16replysize)
        return FALSE;

    U8replylen = pU8reply[5];

    if ((UINT32)U8replylen+8!=U32replylen)
        return FALSE;

    for (count = 0, U8replychk=0; count < U8replylen+7; count++)
      U8replychk ^= pU8reply[count];

    if (pU8reply[U8replylen+7]!=U8replychk)
        return FALSE;

    return TRUE;
}


/********************************************************
*                                                       *
* FUNCTION NAME:                                        *
*                                                       *
* ARGUMENTS:                                            *
*                                                       *
* ARGUMENT NAME:                                        *
*                                                       *
* TYPE:                                                 *
*                                                       *
* I/O:                                                  *
*                                                       *
* DESCRIPTION                                           *
*                                                       *
*                                                       *
* RETURNS:                                              *
*                                                       *
*********************************************************/

static void MBUS_createack(UINT8 *pU8packet, UINT8 *request, UINT32 *U32acklen)
{
  SINT16 count;
  UINT8 U8current = 4, U8packetlen = pU8packet[5];

  request[0]=pU8packet[0];
  request[1]=pU8packet[2];
  request[2]=pU8packet[1];
  request[3]=FBUS_FRTYPE_ACK;

  request[U8current++] = pU8packet[6+U8packetlen]; /* sequence number */

  /* Checksum */
  request[U8current] = 0;
  for (count = 0; count < U8current; count++)
   request[U8current] ^= request[count];

  *U32acklen = (UINT32)U8current;
}


/********************************************************
*                                                       *
* FUNCTION NAME:                                        *
*                                                       *
* ARGUMENTS:                                            *
*                                                       *
* ARGUMENT NAME:                                        *
*                                                       *
* TYPE:                                                 *
*                                                       *
* I/O:                                                  *
*                                                       *
* DESCRIPTION                                           *
*                                                       *
*                                                       *
* RETURNS:                                              *
*                                                       *
*********************************************************/

static UINT32 MBUS_createframe(UINT8 U8seqnum, UINT16 message_length, UINT8 U8message_type, UINT8 *buffer, UINT8 *out_buffer)
{
  SINT16 count, current=0;
  UINT8 checksum;

  if (message_length>MBUS_MAX_CONTENT_LENGTH)
    return FALSE;

  /* Now construct the message header. */

  out_buffer[current++] = MBUS_FRAME_ID;    /* Start of the frame indicator */
  out_buffer[current++] = FBUS_DEVICE_PHONE; /* Destination */
  out_buffer[current++] = MBUS_DEVICE_PC1;    /* Source */
  out_buffer[current++] = U8message_type; /* Type */
  out_buffer[current++] = message_length/256; /* Length1+1 (seq num) */
  out_buffer[current++] = message_length%256; /* Length2+1 (seq num) */

  /* Copy in data if any. */

  if (message_length != 0) {
    memcpy(out_buffer + current, buffer, message_length);
    current+=message_length;
    out_buffer[current] = U8seqnum; /* copy sequence number */
    current++;
  }

  /* Now calculate checksum over entire message and append to message. */

  /* All bytes */
  checksum = 0;
  for (count = 0; count < current; count++)
    checksum ^= out_buffer[count];

  out_buffer[current++] = checksum;

  return current;
}

⌨️ 快捷键说明

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