/*************************************** * ************************ Copyright (c) 2014, tianyuan * All Rights Reserved. ** file name: Standard. H * File ID: Sample Code of the programming specification ** current version: V1.0 * wuyq * completion date: 20140709 ** modify record 1: // modify history, including the modification date, version number, modifier, and modification content. * modify date version number modifier content * required * 20140709 V1.0 wuyq creation ************* ******************************** * *******************/# Ifndef _ standard_h __# DEFINE _ standard_h _/* redefinition of some basic data type */typedef char S8; typedef unsigned char u8; typedef signed short S16; typedef unsigned short 2010; typedef int s32; typedef unsigned int u32; typedef float f32; typedef signed long s64; typedef unsigned long u64; typedef Enum {false = 0, true =! False} bool; typedef bool;/* message header */typedef struct {2010u16msgtype;/* Message Type */service.u16msglength;/* valid message data length */u8 u8transtype; /* transmission channel 0: Network 1: Serial Port */u8 u8reserved [3];/* alignment */} stru_msg_head;/* time information */typedef struct {2010u16year; /* year */2010u16month;/* month */u8 u8day;/* day */u8 u8hour;/* hour */u8 u8minute;/* minute */u8 u8second; /* seconds */} stru_time_info;/* MCM-> main query system information */struct stru_machine_info_req {stru_msg_head strumsgheader;/* message header */}; /* Main-> MCM feedback system information */struct stru_machine_info_rsp {stru_msg_head strumsgheader;/* message header */f32 f32temp;/* Current sampling temperature */f32 f32vol; /* Current Sampling voltage */f32 f32cpufreq;/* CPU frequency */u32 u32freemem;/* remaining memory */u32 u32freedisk;/* remaining flash space */}; /* main returns the software upgrade result to MCM */struct stru_software_update_rsp {stru_msg_head strumsgheader; u8 b8successful;/* indicates whether the upgrade is successful */u8 u8reserved [3, retain */}; // use the following style for the function Header /***************************** **************************************** ** function description: * input parameter: * output parameter: * return value: * Other Description: * modify record 1: // modify history, including the modification date, version number, modifier, and modification content. * modify date version number modifier content * required * 20140709 V1.0 wuyq creation ************* **************************************** * *****************/# endif
/*************************************** * ******************************* Copyright (c) 2014, tianyuan * All Rights Reserved. ** file name: unittest. C * File ID: none * Content summary: Protocol and unit test sample code * Other Instructions: none * Current version: V1.0 * Author: wuyq * completion date: 20140709 ** modify record 1: // modify history, including the modification date, version number, modifier, and modification content. * modify date version number modifier content * required * 20140709 V1.0 wuyq creation ********** **************************************** * *******************/# Include <stdio. h> # include <string. h> // redefine the Data Type typedef unsigned char uint8; typedef unsigned short int uint16; typedef unsigned int uint32; typedef signed int int32; // message header structure typedef struct {uint16 ireserve1; uint16 ireserve2; uint16 ireserve3; uint16 ireserve4;} msghead_t; // message structure (including message header and Message Body) typedef struct {msghead_t msghead; // message header uint32 Iopertype; // operation type. The operation type can only be 1 or 2 uint8 szusernumber [30]; // user number uint8 szopertime [20]; // operation time, in the format: yyyymmdd uint32 ireserve1; // reserved field 1 uint8 szreserve2 [50]; // reserved field 2} userreqmsg_t; // function declaration int32 procuserreqmsg (userreqmsg_t * ptuserreqmsg); int32 main (); /*************************************** * ******************************** function description: main function * input parameter: none * output parameter: none * return value: 0-execution completed * Other Description: No * modification date version number modifier modified content *----------- --------------------------------------------------------------------------------------- * 20140507 V1.0 zzx create ********************************** * ***********************************/int32 main () {uint8 iretval = 0; uint32 iopertype = 0; // operation type uint8 szusernumber [30] = {0}; // user number uint8 szopertime [10] = {0 }; // operation time, in the format of yyyymmdd userreqmsg_t tuserreqmsg = {0}; // request message // assign a value to the Message Header tuserreqmsg. ms Ghead. ireserve1 = 1; tuserreqmsg. msghead. ireserve2 = 2; tuserreqmsg. msghead. ireserve3 = 3; tuserreqmsg. msghead. ireserve4 = 4; // read the value of the Message Field printf ("Operation Type: \ n"); scanf ("% d", & iopertype); printf ("user number: \ n "); scanf (" % s ", szusernumber); printf (" Operation Time: \ n "); scanf (" % s ", szopertime ); // assign values to specific message fields (retain fields without assigning values) tuserreqmsg. iopertype = iopertype; strncpy (tuserreqmsg. szusernumber, szusernumber, strlen (szusernu (Mber); // obtain the number. Use strncpy instead of strcpy strncpy (tuserreqmsg. szopertime, szopertime, strlen (szopertime); // obtain the time. Use strncpy to replace strcpy. // perform an exception judgment on the message body field. iretval = procuserreqmsg (& tuserreqmsg ); // Note: When passing parameters, add & if (iretval = 0) // The function execution is correct {// print the message field content printf ("the user request message is: iopertype = % d, szusernumber = % s, szopertime = % S. \ n ", tuserreqmsg. iopertype, tuserreqmsg. szusernumber, tuserreqmsg. szopertime ); Return 0;} else // print the exception message {printf ("some content of the user request message is wrong, please check! \ N "); Return-1 ;}} /*************************************** * ******************************** function description: exception judgment on the fields of the message body * input parameter: ptuserreqmsg-user request message * output parameter: none * return value: 0-success other-failure * Other description: no * modified date version number modifier modified content * found * 20140507 V1.0 zzx create ************************ **************************************** * *****/int32 procuserreq MSG (userreqmsg_t * ptuserreqmsg) {int32 iretvalue = 0; // exception judgment on input parameters if (ptuserreqmsg = NULL) {printf ("procuserreqmsg (...): input parameter (ptuserreqmsg) is null. \ n "); Return-1 ;}// exception judgment on the message body field if (ptuserreqmsg-> iopertype! = 1) & (ptuserreqmsg-> iopertype! = 2) // The operation type can only be 1 or 2, and the other is Data Exception {printf ("procuserreqmsg (...): The iopertype is wrong, iopertype = % d. \ n ", ptuserreqmsg-> iopertype); Return-2;} If (strlen (ptuserreqmsg-> szusernumber )! = 8) // The user number is incorrect. The length is 8 characters. {printf ("procuserreqmsg (...): The szusernumber is wrong. \ n "); Return-3;} If (strlen (ptuserreqmsg-> szopertime )! = 8) // The operation time is abnormal. The length is 8 bits. {printf ("procuserreqmsg (...): The szopertime is wrong. \ n "); Return-4;} return 0 ;}