├── chapter3 ├── 02-adapter │ ├── GasMix.h │ ├── main │ ├── AcmeO2SensorProxy.c │ ├── UltimateO2SensorProxy.c │ ├── GasMixer.h │ ├── GasDisplay.h │ ├── main.c │ ├── iO2Sensor.h │ ├── AcmeO2SensorProxy.h │ ├── UltimateO2SensorProxy.h │ ├── GasDisplay.c │ ├── GasMixer.c │ ├── AcmeO2Adapter.h │ ├── UltimateO2Adapter.h │ ├── AcmeO2Adapter.c │ └── UltimateO2Adapter.c ├── 04-observer │ ├── GasMixerClient.c │ ├── GasMixerClient.h │ ├── SatetyMonitorClient.c │ ├── SatetyMonitorClient.h │ ├── main │ ├── GasData.c │ ├── GasData.h │ ├── GasNotificationHandle.h │ ├── GasNotificationHandle.c │ ├── main.c │ ├── DisplayClient.h │ └── GasSensor.h ├── 07-poll │ ├── main │ ├── main.c │ ├── BreathingCircuitSensor.h │ ├── MedicalDisplay.h │ ├── BCTimer.h │ ├── BreathingCircuitSensor.c │ ├── BCTimer.c │ ├── MedicalDisplay.c │ └── BCPeriodicPoller.h ├── 05-debounce │ ├── main │ ├── Timer.h │ ├── Timer.c │ ├── MicrowaveEmitter.h │ ├── Button.h │ ├── MicrowaveEmitter.c │ ├── Button.c │ ├── main.c │ └── ButtonDriver.h ├── 01-hardwareproxy │ ├── main │ ├── bad │ │ ├── bitfield │ │ ├── bitmapping │ │ ├── bitfield.c │ │ └── bitmapping.c │ ├── HWProxyExample.h │ ├── MotorData.h │ ├── maintest.c │ └── MotorProxy.h ├── 03-intermediary │ ├── main │ ├── SlidingArmJoint.c │ ├── iSlidingJoint.h │ ├── iRotatingJoint.h │ ├── RotatingArmJoint.c │ ├── GraspingManipulator.c │ ├── RotatingArmJoint.h │ ├── GraspingManipulator.h │ ├── Action.c │ ├── SlidingArmJoint.h │ ├── Action.h │ └── main.c └── 06-interupt │ ├── RobotInterruptVectorTable.h │ ├── LED.h │ ├── ButtonHandler.h │ ├── main.c │ ├── LED.c │ └── ButtonHandler.c ├── chapter4 ├── 03-critical │ ├── CRDisplay.h │ ├── RobotArm.h │ ├── UserInput.h │ └── CRRobotArmManager.h ├── 01-loop │ ├── SharedData.c │ ├── GasDisplayThread.h │ ├── SharedData.h │ ├── GasDisplayThread.c │ ├── GasControlExecutive.c │ ├── loop1.c │ └── loop2.c ├── 08-orderLock │ ├── Velocity.c │ ├── Velocity.h │ ├── ResourceList.h │ ├── VelocitySensor.h │ └── ResourceList.c ├── 02-staticpriority │ ├── MotorDisplay.h │ ├── MotorController.h │ ├── MotorData.h │ ├── MotorPositionSensor.h │ ├── main.c │ ├── MotorData.c │ ├── MotorDisplay.c │ ├── MotorPositionSensor.c │ └── MotorController.c ├── 05-queue │ ├── GasData.h │ ├── GasData.c │ ├── HeSensor.h │ ├── N2Sensor.h │ ├── O2Sensor.h │ ├── SensorThread.h │ ├── GasDisplay.h │ ├── HeSensor.c │ ├── N2Sensor.c │ ├── O2Sensor.c │ ├── QueuingExample.h │ ├── GasController.h │ ├── GasDataQueue.h │ ├── GasDisplay.c │ ├── GasController.c │ └── SensorThread.c ├── 04-guard │ ├── GuardedCallExample.h │ ├── Navigator.h │ ├── Attitude.h │ ├── FlightDataDisplay.h │ ├── Position.h │ ├── KinematicData.h │ ├── AttitudeController.h │ ├── Attitude.c │ ├── Position.c │ ├── Navigator.c │ ├── AttitudeController.c │ ├── FlightDataDisplay.c │ └── KinematicData.c ├── 07-simulaLock │ ├── OpticalSpeedSensor.c │ ├── DopplerSpeedSensor.h │ ├── OpticalSpeedSensor.h │ ├── GPSPositionSensor.h │ ├── GPSPositionSensor.c │ └── DopplerSpeedSensor.c └── 06-ThreadBarrier │ ├── ThreadBarrier.h │ └── ThreadBarrier.c ├── chapter1 ├── 02-Queue │ ├── Queue │ ├── QueueTest.c │ ├── Queue.h │ ├── CachedQueue.h │ └── Queue.c ├── 01-Sensor │ ├── Sensor │ ├── SensorTest.c │ ├── Sensor.h │ └── Sensor.c └── 03-SecuritySupervisor │ ├── SecuritySupervisor │ ├── SecuritySupervisor.h │ └── SecuritySupervisorTest.c ├── chapter2 ├── 02-observer │ ├── main │ ├── TimeMarkedData.c │ ├── ECGPkg.h │ ├── TimeMarkedData.h │ ├── ECG_Module.h │ ├── NotificationHandle.h │ ├── HistogramDisplay.h │ ├── NotificationHandle.c │ ├── TestBuilder.h │ ├── ECG_Module.c │ ├── HistogramDisplay.c │ ├── TMDQueue.h │ └── main.c └── 01-client-server │ ├── main │ ├── TimeMarkedData.c │ ├── ECGPkg.h │ ├── TimeMarkedData.h │ ├── WaveformDisplay.h │ ├── WaveformDisplay.c │ ├── ECG_Module.h │ ├── QRSDetector.h │ ├── HistogramDisplay.h │ ├── ArrhythmiaDetector.h │ ├── TMDQueue.h │ ├── ArrhythmiaDetector.c │ ├── TestBuilder.h │ ├── QRSDetector.c │ ├── ECG_Module.c │ ├── HistogramDisplay.c │ └── main.c ├── chapter5 ├── 04-StatePattern │ ├── SPClient.h │ ├── StatePatternBulder.h │ ├── StatePattern.h │ ├── StateActionSet.h │ ├── StateActionSet.c │ ├── Context.h │ ├── NoNumberState.h │ ├── GN_ProcessingWholeState.h │ └── GN_ProcessingFractionState.h ├── 05-AndState │ ├── Klaxon.h │ ├── DecompClient.h │ ├── DecompBuilder.h │ ├── AndStateList.h │ ├── Light.h │ ├── AndStateList.c │ ├── StateActionSet.h │ ├── StateActionSet.c │ ├── ModeStateClass.h │ ├── ErrorStateClass.h │ └── LightPkg.h ├── 01-singleReceptor │ ├── TokenizeAsyncSignlePkg.h │ ├── TSREventQueue.h │ └── TSRSyncSingleReceptor.h └── 03-StateTable │ ├── StateTablePattern.h │ └── TokenizerStateTable.h └── chapter6 ├── 05-protectsinglechannel ├── FurnaceController.h ├── CheckTemperature.h ├── Thermostat.h ├── Thermometer.h ├── CheckTemperature.c ├── Thermostat.c └── Thermometer.c ├── 01-OwnShipAttitude ├── AlarmManager.h ├── AttitudeDataType.h ├── OwnShipAttitude.h └── OwnShipAttitude.c ├── 02-crc ├── AlarmManager.h ├── CRCExample.h └── PatientData.h ├── 04-channelmode ├── LightDeviceDriver.h ├── CalculateColor.h ├── ConvertToFrequency.h ├── MovingAverageFilter.h ├── EMGSensorDeviceDriver.h └── EMGChannel.h └── 03-SmartData ├── SmartDataExample.h ├── SmartInt.h ├── PatientDataClass.h └── SmartColor.h /chapter3/02-adapter/GasMix.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chapter4/03-critical/CRDisplay.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chapter4/03-critical/RobotArm.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chapter4/03-critical/UserInput.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chapter3/04-observer/GasMixerClient.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chapter3/04-observer/GasMixerClient.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chapter3/04-observer/SatetyMonitorClient.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chapter3/04-observer/SatetyMonitorClient.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chapter4/01-loop/SharedData.c: -------------------------------------------------------------------------------- 1 | #include "SharedData.h" 2 | 3 | int commandedGasFlow; 4 | int measuredGasFlow; 5 | -------------------------------------------------------------------------------- /chapter4/08-orderLock/Velocity.c: -------------------------------------------------------------------------------- 1 | #include "Velocity.h" 2 | 3 | void Velocity_Init(Velocity* const me){ 4 | 5 | } 6 | -------------------------------------------------------------------------------- /chapter1/02-Queue/Queue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sundaygeek/design-patterns-for-embedded-system-in-c/HEAD/chapter1/02-Queue/Queue -------------------------------------------------------------------------------- /chapter3/07-poll/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sundaygeek/design-patterns-for-embedded-system-in-c/HEAD/chapter3/07-poll/main -------------------------------------------------------------------------------- /chapter1/01-Sensor/Sensor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sundaygeek/design-patterns-for-embedded-system-in-c/HEAD/chapter1/01-Sensor/Sensor -------------------------------------------------------------------------------- /chapter2/02-observer/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sundaygeek/design-patterns-for-embedded-system-in-c/HEAD/chapter2/02-observer/main -------------------------------------------------------------------------------- /chapter3/02-adapter/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sundaygeek/design-patterns-for-embedded-system-in-c/HEAD/chapter3/02-adapter/main -------------------------------------------------------------------------------- /chapter3/04-observer/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sundaygeek/design-patterns-for-embedded-system-in-c/HEAD/chapter3/04-observer/main -------------------------------------------------------------------------------- /chapter3/05-debounce/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sundaygeek/design-patterns-for-embedded-system-in-c/HEAD/chapter3/05-debounce/main -------------------------------------------------------------------------------- /chapter2/01-client-server/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sundaygeek/design-patterns-for-embedded-system-in-c/HEAD/chapter2/01-client-server/main -------------------------------------------------------------------------------- /chapter3/01-hardwareproxy/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sundaygeek/design-patterns-for-embedded-system-in-c/HEAD/chapter3/01-hardwareproxy/main -------------------------------------------------------------------------------- /chapter3/03-intermediary/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sundaygeek/design-patterns-for-embedded-system-in-c/HEAD/chapter3/03-intermediary/main -------------------------------------------------------------------------------- /chapter4/01-loop/GasDisplayThread.h: -------------------------------------------------------------------------------- 1 | #ifndef GasDisplayThread_H 2 | #define GasDisplayThread_H 3 | 4 | void GasDisplayThread_run(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /chapter3/01-hardwareproxy/bad/bitfield: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sundaygeek/design-patterns-for-embedded-system-in-c/HEAD/chapter3/01-hardwareproxy/bad/bitfield -------------------------------------------------------------------------------- /chapter3/01-hardwareproxy/bad/bitmapping: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sundaygeek/design-patterns-for-embedded-system-in-c/HEAD/chapter3/01-hardwareproxy/bad/bitmapping -------------------------------------------------------------------------------- /chapter4/01-loop/SharedData.h: -------------------------------------------------------------------------------- 1 | #ifndef SharedData_H 2 | #define SharedData_H 3 | 4 | extern int commandedGasFlow; 5 | extern int measuredGasFlow; 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /chapter1/03-SecuritySupervisor/SecuritySupervisor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sundaygeek/design-patterns-for-embedded-system-in-c/HEAD/chapter1/03-SecuritySupervisor/SecuritySupervisor -------------------------------------------------------------------------------- /chapter3/02-adapter/AcmeO2SensorProxy.c: -------------------------------------------------------------------------------- 1 | #include "AcmeO2SensorProxy.h" 2 | 3 | unsigned int getO2Conc(){ 4 | return 5; 5 | } 6 | 7 | unsigned long getO2Flow(){ 8 | return 5; 9 | } 10 | -------------------------------------------------------------------------------- /chapter5/04-StatePattern/SPClient.h: -------------------------------------------------------------------------------- 1 | #ifndef SPClient_H 2 | #define SPClient_H 3 | 4 | typedef struct SPClient SPClient; 5 | struct SPClient 6 | { 7 | 8 | }; 9 | 10 | 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /chapter3/02-adapter/UltimateO2SensorProxy.c: -------------------------------------------------------------------------------- 1 | #include "UltimateO2SensorProxy.h" 2 | 3 | unsigned int accessO2Conc(){ 4 | return 2; 5 | } 6 | 7 | unsigned long accessGasFlow(){ 8 | return 2; 9 | } 10 | -------------------------------------------------------------------------------- /chapter3/02-adapter/GasMixer.h: -------------------------------------------------------------------------------- 1 | #ifndef GasMixer_H 2 | #define GasMixer_H 3 | 4 | #include "iO2Sensor.h" 5 | #include "AcmeO2Adapter.h" 6 | #include "UltimateO2Adapter.h" 7 | 8 | void mixerGas(); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /chapter3/06-interupt/RobotInterruptVectorTable.h: -------------------------------------------------------------------------------- 1 | #ifndef RobotInterruptVectorTable_H 2 | #define RobotInterruptVectorTable_H 3 | 4 | typedef void* ButtonVectorPtr; 5 | 6 | ButtonVectorPtr ISRAddress[10]; 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /chapter3/02-adapter/GasDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef GasDisplay_H 2 | #define GasDisplay_H 3 | 4 | #include "iO2Sensor.h" 5 | #include "AcmeO2Adapter.h" 6 | #include "UltimateO2Adapter.h" 7 | 8 | void displayGas(); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /chapter3/02-adapter/main.c: -------------------------------------------------------------------------------- 1 | #include "GasMixer.h" 2 | #include "GasDisplay.h" 3 | #include 4 | 5 | int main(int argc, char const *argv[]) 6 | { 7 | displayGas(); 8 | mixerGas(); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /chapter4/08-orderLock/Velocity.h: -------------------------------------------------------------------------------- 1 | #ifndef Velocity_H 2 | #define Velocity_H 3 | 4 | typedef struct Velocity Velocity; 5 | struct Velocity 6 | { 7 | 8 | }; 9 | 10 | void Velocity_Init(Velocity* const me); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /chapter4/01-loop/GasDisplayThread.c: -------------------------------------------------------------------------------- 1 | #include "GasDisplayThread.h" 2 | 3 | void GasDisplayThread_run(void) { 4 | printf("Measured Gas Flow %d\n", measuredGasFlow); 5 | printf("commanded Gas Flow %d\n\n", commandedGasFlow); 6 | } 7 | -------------------------------------------------------------------------------- /chapter5/04-StatePattern/StatePatternBulder.h: -------------------------------------------------------------------------------- 1 | #ifndef StatePatternBulder_H 2 | #define StatePatternBulder_H 3 | 4 | typedef struct StatePatternBulder StatePatternBulder; 5 | struct StatePatternBulder 6 | { 7 | 8 | }; 9 | 10 | 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /chapter3/02-adapter/iO2Sensor.h: -------------------------------------------------------------------------------- 1 | #ifndef IO2Sensor_H 2 | #define IO2Sensor_H 3 | 4 | typedef struct iO2Sensor iO2Sensor; 5 | struct iO2Sensor 6 | { 7 | int (*gimmeO2Flow)(void); 8 | int (*gimmeO2Conc)(void); 9 | }; 10 | 11 | #endif // IO2Sensor_H 12 | -------------------------------------------------------------------------------- /chapter3/03-intermediary/SlidingArmJoint.c: -------------------------------------------------------------------------------- 1 | #include "SlidingArmJoint.h" 2 | 3 | int SlidingArmJoint_getLength(SlidingArmJoint*const me){ 4 | } 5 | int SlidingArmJoint_setLength(SlidingArmJoint*const me, int x){ 6 | } 7 | int SlidingArmJoint_zero(SlidingArmJoint*const me){ 8 | } 9 | 10 | -------------------------------------------------------------------------------- /chapter3/03-intermediary/iSlidingJoint.h: -------------------------------------------------------------------------------- 1 | #ifndef ISlidingJoint_H 2 | #define ISlidingJoint_H 3 | 4 | typedef struct iSlidingJoint iSlidingJoint; 5 | struct iSlidingJoint 6 | { 7 | int (*getLength)(); 8 | void (*setLength)(int x); 9 | int (*zero)(); 10 | }; 11 | #endif 12 | -------------------------------------------------------------------------------- /chapter3/03-intermediary/iRotatingJoint.h: -------------------------------------------------------------------------------- 1 | #ifndef IRoatingJoint_H 2 | #define IRoatingJoint_H 3 | 4 | typedef struct iRoatingJoint iRoatingJoint; 5 | struct iRoatingJoint 6 | { 7 | int (*getRotation)(); 8 | int (*rotate)(int x); 9 | int (*zero)(); 10 | }; 11 | 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /chapter4/02-staticpriority/MotorDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef MotorDisplay_H 2 | #define MotorDisplay_H 3 | 4 | void MotorDisplay_Init(void); 5 | void MotorDisplay_Cleanup(void); 6 | 7 | void MotorDisplay_run(void); 8 | void display(void); 9 | 10 | void MotorDisplay_initRelations(void); 11 | #endif 12 | -------------------------------------------------------------------------------- /chapter3/07-poll/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "BCPeriodicPoller.h" 4 | 5 | int main(int argc, char const *argv[]) 6 | { 7 | BCPeriodicPoller* p_BCPeriodicPoller = BCPeriodicPoller_Create(); 8 | BCPeriodicPoller_poll(p_BCPeriodicPoller); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /chapter3/03-intermediary/RotatingArmJoint.c: -------------------------------------------------------------------------------- 1 | #include "RotatingArmJoint.h" 2 | 3 | int RotatingArmJoint_getRotation(RotatingArmJoint* const me){ 4 | return me->angle; 5 | } 6 | int RotatingArmJoint_rotate(RotatingArmJoint* const me, int x){ 7 | } 8 | int RotatingArmJoint_zero(RotatingArmJoint* const me){ 9 | } 10 | -------------------------------------------------------------------------------- /chapter3/03-intermediary/GraspingManipulator.c: -------------------------------------------------------------------------------- 1 | #include "GraspingManipulator.h" 2 | 3 | int GraspingManipulator_setMaxForce(GraspingManipulator* const me, int maxForce){ 4 | } 5 | 6 | int GraspingManipulator_open(GraspingManipulator* me){ 7 | } 8 | 9 | int GraspingManipulator_close(GraspingManipulator* me){ 10 | } 11 | 12 | -------------------------------------------------------------------------------- /chapter3/01-hardwareproxy/HWProxyExample.h: -------------------------------------------------------------------------------- 1 | #ifndef HWProxyExample_H 2 | #define HWProxyExample_H 3 | 4 | struct MotorController; 5 | struct MotorData; 6 | struct MotorDisplay; 7 | struct MotorProxy; 8 | 9 | typedef enum DirectionType { 10 | NO_DIRECTION, 11 | FORWARD, 12 | REVERSE 13 | } DirectionType; 14 | 15 | #endif -------------------------------------------------------------------------------- /chapter4/02-staticpriority/MotorController.h: -------------------------------------------------------------------------------- 1 | #ifndef MotorController_H 2 | #define MotorController_H 3 | 4 | void MotorController_Init(void); 5 | void MotorController_Cleanup(void); 6 | 7 | void MotorController_run(void); 8 | void move(void); 9 | void zero(void); 10 | 11 | void MotorController_initRelations(void); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /chapter4/02-staticpriority/MotorData.h: -------------------------------------------------------------------------------- 1 | #ifndef MotorData_H 2 | #define MotorData_H 3 | 4 | #define RETRIGGER (2) 5 | #define WAIT_FOREVER (0) 6 | 7 | extern int commandedPosition; 8 | extern int measuredPosition; 9 | 10 | int getCmdPos(void); 11 | int getMeasPos(void); 12 | void setCmdPos(int x); 13 | void setMeasPos(int x); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /chapter4/02-staticpriority/MotorPositionSensor.h: -------------------------------------------------------------------------------- 1 | #ifndef MotorPositionSensor_H 2 | #define MotorPositionSensor_H 3 | 4 | void MotorPositionSensor_Init(void); 5 | void MotorPositionSensor_Cleanup(void); 6 | 7 | void MotorPositionSensor_run(void); 8 | void getPosition(void); 9 | 10 | void MotorPositionSensor_initRelations(void); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /chapter2/02-observer/TimeMarkedData.c: -------------------------------------------------------------------------------- 1 | #include "TimeMarkedData.h" 2 | 3 | void TimeMarkedData_Init(TimeMarkedData* const me){ 4 | 5 | } 6 | 7 | 8 | void TimeMarkedData_Cleanup(TimeMarkedData* const me){ 9 | 10 | } 11 | 12 | void TimeMarkedData_setItsTMDQueue(TimeMarkedData* const me, struct TMDQueue* p_TMDQueue){ 13 | me->itsTMDQueue = p_TMDQueue; 14 | } 15 | -------------------------------------------------------------------------------- /chapter2/01-client-server/TimeMarkedData.c: -------------------------------------------------------------------------------- 1 | #include "TimeMarkedData.h" 2 | 3 | void TimeMarkedData_Init(TimeMarkedData* const me){ 4 | 5 | } 6 | 7 | 8 | void TimeMarkedData_Cleanup(TimeMarkedData* const me){ 9 | 10 | } 11 | 12 | void TimeMarkedData_setItsTMDQueue(TimeMarkedData* const me, struct TMDQueue* p_TMDQueue){ 13 | me->itsTMDQueue = p_TMDQueue; 14 | } 15 | -------------------------------------------------------------------------------- /chapter3/02-adapter/AcmeO2SensorProxy.h: -------------------------------------------------------------------------------- 1 | #ifndef AcmeO2SensorProxy_H 2 | #define AcmeO2SensorProxy_H 3 | 4 | typedef struct AcmeO2SensorProxy AcmeO2SensorProxy; 5 | struct AcmeO2SensorProxy 6 | { 7 | unsigned int (*getO2Conc)(void); 8 | unsigned long (*getO2Flow)(void); 9 | }; 10 | 11 | unsigned int getO2Conc(); 12 | unsigned long getO2Flow(); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /chapter4/02-staticpriority/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main(void) { 4 | /* initialize the links and start the tasks */ 5 | MotorPositionSensor_initRelations(); 6 | MotorDisplay_initRelations(); 7 | MotorController_initRelations(); 8 | 9 | /* now let the tasks do their thing */ 10 | while (TRUE) { 11 | task(); 12 | }; 13 | }; 14 | -------------------------------------------------------------------------------- /chapter5/05-AndState/Klaxon.h: -------------------------------------------------------------------------------- 1 | #ifndef Klaxon_H 2 | #define Klaxon_H 3 | 4 | typedef struct Klaxon Klaxon; 5 | struct Klaxon 6 | { 7 | 8 | }; 9 | 10 | void Klaxon_Init(Klaxon* const me); 11 | void Klaxon_Cleanup(Klaxon* const me); 12 | Klaxon * Klaxon_Create(void); 13 | void Klaxon_Destroy(Klaxon* const me); 14 | 15 | 16 | void Klaxon_alarmOn(Klaxon* const me); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /chapter6/05-protectsinglechannel/FurnaceController.h: -------------------------------------------------------------------------------- 1 | #ifndef FurnaceController_H 2 | #define FurnaceController_H 3 | 4 | typedef struct FurnaceController FurnaceController; 5 | struct FurnaceController 6 | { 7 | 8 | }; 9 | 10 | void FurnaceController_emergencyRestart(FurnaceController* const me); 11 | void FurnaceController_shutDown(FurnaceController* const me); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /chapter6/01-OwnShipAttitude/AlarmManager.h: -------------------------------------------------------------------------------- 1 | #ifndef AlertManager_H 2 | #define AlertManager_H 3 | 4 | typedef struct AlarmManager AlarmManager; 5 | struct AlarmManager 6 | { 7 | 8 | }; 9 | 10 | void AlarmManager_Init(AlarmManager* const me); 11 | void AlarmManager_Cleanup(AlarmManager* const me); 12 | AlarmManager* AlarmManager_Create(void); 13 | void AlarmManager_Destroy(AlarmManager* const me); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /chapter4/02-staticpriority/MotorData.c: -------------------------------------------------------------------------------- 1 | #include "MotorData.h" 2 | 3 | int commandedPosition = 0; 4 | int measuredPosition = 0; 5 | 6 | int getCmdPos(void) { 7 | return commandedPosition; 8 | } 9 | 10 | int getMeasPos(void) { 11 | return measuredPosition; 12 | } 13 | 14 | void setCmdPos(int x) { 15 | commandedPosition = x; 16 | } 17 | 18 | void setMeasPos(int x) { 19 | measuredPosition = x; 20 | } 21 | -------------------------------------------------------------------------------- /chapter5/05-AndState/DecompClient.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef DecompClient_H 3 | #define DecompClient_H 4 | 5 | typedef struct DecompClient DecompClient; 6 | struct DecompClient 7 | { 8 | 9 | }; 10 | 11 | void DecompClient_Init(DecompClient* const me); 12 | void DecompClient_Cleanup(DecompClient* const me); 13 | DecompClient * DecompClient_Create(void); 14 | void DecompClient_Destroy(DecompClient* const me); 15 | 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /chapter5/05-AndState/DecompBuilder.h: -------------------------------------------------------------------------------- 1 | #ifndef DecompBuilder_H 2 | #define DecompBuilder_H 3 | 4 | typedef struct DecompBuilder DecompBuilder; 5 | struct DecompBuilder 6 | { 7 | 8 | }; 9 | 10 | void DecompBuilder_Init(DecompBuilder* const me); 11 | void DecompBuilder_Cleanup(DecompBuilder* const me); 12 | DecompBuilder * DecompBuilder_Create(void); 13 | void DecompBuilder_Destroy(DecompBuilder* const me); 14 | 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /chapter3/02-adapter/UltimateO2SensorProxy.h: -------------------------------------------------------------------------------- 1 | #ifndef UltimateO2SensorProxy_H 2 | #define UltimateO2SensorProxy_H 3 | 4 | typedef struct UltimateO2SensorProxy UltimateO2SensorProxy; 5 | struct UltimateO2SensorProxy 6 | { 7 | unsigned int (*accessO2Conc)(void); 8 | unsigned long (*accessGasFlow)(void); 9 | }; 10 | 11 | unsigned int accessO2Conc(); 12 | unsigned long accessGasFlow(); 13 | 14 | #endif // UltimateO2SensorProxy_H 15 | -------------------------------------------------------------------------------- /chapter3/05-debounce/Timer.h: -------------------------------------------------------------------------------- 1 | #ifndef TIMER_H 2 | #define TIMER_H 3 | 4 | #include 5 | #include 6 | 7 | typedef struct Timer Timer; 8 | struct Timer 9 | { 10 | int time; 11 | }; 12 | 13 | void Timer_Init(Timer* const me); 14 | void Timer_Cleanup(Timer* const me); 15 | 16 | Timer* Timer_Create(); 17 | void Timer_Destroy(Timer* const me); 18 | 19 | void Timer_delay(Timer* const me, int time); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /chapter3/03-intermediary/RotatingArmJoint.h: -------------------------------------------------------------------------------- 1 | #ifndef RotatingArmJoint_H 2 | #define RotatingArmJoint_H 3 | 4 | typedef struct RotatingArmJoint RotatingArmJoint; 5 | struct RotatingArmJoint 6 | { 7 | int angle; 8 | char deviceID; 9 | }; 10 | 11 | int RotatingArmJoint_getRotation(RotatingArmJoint* const me); 12 | int RotatingArmJoint_rotate(RotatingArmJoint* const me, int x); 13 | int RotatingArmJoint_zero(RotatingArmJoint* const me); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /chapter3/03-intermediary/GraspingManipulator.h: -------------------------------------------------------------------------------- 1 | #ifndef GraspingManipulator_H 2 | #define GraspingManipulator_H 3 | 4 | typedef struct GraspingManipulator GraspingManipulator; 5 | struct GraspingManipulator 6 | { 7 | int maxForce; 8 | }; 9 | 10 | int GraspingManipulator_setMaxForce(GraspingManipulator* const me, int maxForce); 11 | int GraspingManipulator_open(GraspingManipulator* me); 12 | int GraspingManipulator_close(GraspingManipulator* me); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /chapter4/01-loop/GasControlExecutive.c: -------------------------------------------------------------------------------- 1 | #include "GasControlExecutive.h" 2 | #include "GasActuatorThread.h" 3 | #include "GasControlEpochTimer.h" 4 | #include "GasDisplayThread.h" 5 | #include "GasSensorThread.h" 6 | 7 | void controlLoop(void) { 8 | while (TRUE) { 9 | startEpochTimer(); 10 | GasSensorThread_run(); 11 | GasActuatorThread_run(); 12 | GasDisplayThread_run(); 13 | while(!epochTimerHasElapsed()) ; 14 | }; 15 | } 16 | -------------------------------------------------------------------------------- /chapter3/06-interupt/LED.h: -------------------------------------------------------------------------------- 1 | #ifndef LED_H 2 | #define LED_H 3 | 4 | #include 5 | #include 6 | #define LED_ON 1 7 | #define LED_OFF 0 8 | 9 | typedef struct LED LED; 10 | struct LED 11 | { 12 | int LEDStatus; 13 | }; 14 | 15 | void LED_Init(LED* const me); 16 | void LED_Cleanup(LED* const me); 17 | 18 | LED* LED_Create(); 19 | void LED_Destroy(LED* const me); 20 | 21 | void LED_LightOn(LED* const me); 22 | void LED_LightOff(LED* const me); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /chapter6/02-crc/AlarmManager.h: -------------------------------------------------------------------------------- 1 | #ifndef AlertManager_H 2 | #define AlertManager_H 3 | 4 | typedef struct AlarmManager AlarmManager; 5 | struct AlarmManager 6 | { 7 | 8 | }; 9 | 10 | void AlarmManager_Init(AlarmManager* const me); 11 | void AlarmManager_Cleanup(AlarmManager* const me); 12 | AlarmManager* AlarmManager_Create(void); 13 | void AlarmManager_Destroy(AlarmManager* const me); 14 | 15 | void AlarmManager_addAlarm(AlarmManager* const me, ErrorCodeType errCode); 16 | 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /chapter2/02-observer/ECGPkg.h: -------------------------------------------------------------------------------- 1 | #ifndef ECGPkg_H 2 | #define ECGPkg_H 3 | 4 | // electrocardiogram ECG 5 | 6 | #include 7 | #include 8 | 9 | /* forward declarations */ 10 | struct ArrythmiaDetector; 11 | struct ECG_Module; 12 | struct HistogramDisplay; 13 | struct QRSDetector; 14 | struct TMDQueue; 15 | struct TestBuilder; 16 | struct TimeMarkedData; 17 | struct WaveformDisplay; 18 | 19 | typedef unsigned char boolean; 20 | #define QUEUE_SIZE (20000) 21 | 22 | #endif //ECGPkg_H 23 | -------------------------------------------------------------------------------- /chapter3/03-intermediary/Action.c: -------------------------------------------------------------------------------- 1 | #include "Action.h" 2 | #include 3 | 4 | void Action_Init(Action* me){ 5 | } 6 | void Action_Cleanup(Action* me){ 7 | 8 | } 9 | 10 | Action* Action_Create(){ 11 | Action* me = (Action*)malloc(sizeof(Action)); 12 | if (me != NULL) 13 | { 14 | Action_Init(me); 15 | } 16 | return me; 17 | } 18 | void Action_Destroy(Action* me){ 19 | if (me != NULL) 20 | { 21 | Action_Destroy(me); 22 | } 23 | free(me); 24 | } 25 | -------------------------------------------------------------------------------- /chapter4/01-loop/loop1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main(void) { 4 | /* global static and stack data */ 5 | static int nTasks = 3; 6 | int currentTask; 7 | 8 | /* initialization code */ 9 | currentTask = 0; 10 | 11 | if (POST()) { /* Power On Self Test succeeds */ 12 | /* scheduling executive */ 13 | while (TRUE) { 14 | task1(); 15 | task2(); 16 | task3(); 17 | }; /* end cyclic processing loop */ 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /chapter2/01-client-server/ECGPkg.h: -------------------------------------------------------------------------------- 1 | #ifndef ECGPkg_H 2 | #define ECGPkg_H 3 | 4 | // electrocardiogram ECG 5 | 6 | #include 7 | #include 8 | 9 | /* forward declarations */ 10 | struct ArrythmiaDetector; 11 | struct ECG_Module; 12 | struct HistogramDisplay; 13 | struct QRSDetector; 14 | struct TMDQueue; 15 | struct TestBuilder; 16 | struct TimeMarkedData; 17 | struct WaveformDisplay; 18 | 19 | typedef unsigned char boolean; 20 | #define QUEUE_SIZE (20000) 21 | 22 | #endif //ECGPkg_H 23 | -------------------------------------------------------------------------------- /chapter3/03-intermediary/SlidingArmJoint.h: -------------------------------------------------------------------------------- 1 | #ifndef SlidingArmJoint_H 2 | #define SlidingArmJoint_H 3 | 4 | typedef struct SlidingArmJoint SlidingArmJoint; 5 | struct SlidingArmJoint 6 | { 7 | int currentLength; 8 | int deviceID; 9 | int minArmLength; 10 | int maxArmLength; 11 | }; 12 | 13 | int SlidingArmJoint_getLength(SlidingArmJoint*const me); 14 | int SlidingArmJoint_setLength(SlidingArmJoint*const me, int x); 15 | int SlidingArmJoint_zero(SlidingArmJoint*const me); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /chapter3/04-observer/GasData.c: -------------------------------------------------------------------------------- 1 | #include "GasData.h" 2 | 3 | void GasData_Init(GasData* const me) { 4 | } 5 | 6 | void GasData_Cleanup(GasData* const me) { 7 | } 8 | 9 | GasData * GasData_Create(void) { 10 | GasData* me = (GasData *) malloc(sizeof(GasData)); 11 | if(me!=NULL) { 12 | GasData_Init(me); 13 | } 14 | return me; 15 | } 16 | 17 | void GasData_Destroy(GasData* const me) { 18 | if(me!=NULL) { 19 | GasData_Cleanup(me); 20 | } 21 | free(me); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /chapter3/06-interupt/ButtonHandler.h: -------------------------------------------------------------------------------- 1 | #ifndef ButtonHandler_H 2 | #define ButtonHandler_H 3 | 4 | typedef void (*ButtonVectorPtr)(void); 5 | struct LED; 6 | 7 | extern ButtonVectorPtr oldVectors[10]; 8 | 9 | /* Operations */ 10 | void install(void); 11 | void deinstall(void); 12 | 13 | interrupt void handleButtonPushInterrupt(void); 14 | interrupt void handleButtonReleaseInterrupt(void); 15 | 16 | struct LED* ButtonHandler_getItsLED(void); 17 | void ButtonHandler_setItsLED(struct LED* p_LED); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /chapter3/04-observer/GasData.h: -------------------------------------------------------------------------------- 1 | #ifndef GasData_H 2 | #define GasData_H 3 | 4 | #include 5 | #include 6 | 7 | typedef struct GasData GasData; 8 | struct GasData 9 | { 10 | unsigned short N2Conc; 11 | unsigned short O2Conc; 12 | unsigned int flowRate; 13 | }; 14 | 15 | /* Constructors and destructors:*/ 16 | void GasData_Init(GasData* const me); 17 | void GasData_Cleanup(GasData* const me); 18 | GasData * GasData_Create(void); 19 | void GasData_Destroy(GasData* const me); 20 | 21 | #endif 22 | 23 | -------------------------------------------------------------------------------- /chapter4/05-queue/GasData.h: -------------------------------------------------------------------------------- 1 | #ifndef GasData_H 2 | #define GasData_H 3 | 4 | #include 5 | #include 6 | #include "QueuingExample.h" 7 | 8 | typedef struct GasData GasData; 9 | struct GasData { 10 | double conc; 11 | unsigned int flowInCCPerMin; 12 | GAS_TYPE gType; 13 | }; 14 | 15 | /* Constructors and destructors:*/ 16 | 17 | void GasData_Init(GasData* const me); 18 | void GasData_Cleanup(GasData* const me); 19 | GasData * GasData_Create(void); 20 | void GasData_Destroy(GasData* const me); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /chapter5/05-AndState/AndStateList.h: -------------------------------------------------------------------------------- 1 | #ifndef AndStateList_H 2 | #define AndStateList_H 3 | 4 | #include "LightPkg.h" 5 | 6 | struct ErrorStateClass; 7 | 8 | typedef struct AndStateList AndStateList; 9 | struct AndStateList { 10 | struct ErrorStateClass* andStates[MAX_ANDSTATES]; 11 | int nAndStates; 12 | }; 13 | 14 | void AndStateList_Init(AndStateList* const me); 15 | void AndStateList_Cleanup(AndStateList* const me); 16 | AndStateList * AndStateList_Create(void); 17 | void AndStateList_Destroy(AndStateList* const me); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /chapter5/05-AndState/Light.h: -------------------------------------------------------------------------------- 1 | #ifndef Light_H 2 | #define Light_H 3 | 4 | #include "LightPkg.h" 5 | 6 | typedef struct Light Light; 7 | struct Light 8 | { 9 | 10 | }; 11 | 12 | void Light_Init(Light* const me); 13 | void Light_Cleanup(Light* const me); 14 | Light * Light_Create(void); 15 | void Light_Destroy(Light* const me); 16 | 17 | 18 | void Light_turnOn(Light* const me); 19 | void Light_turnOff(Light* const me); 20 | void Light_setColor(Light* const me, ColorType color); 21 | void Light_setMode(Light* const me, FlashType mode); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /chapter6/04-channelmode/LightDeviceDriver.h: -------------------------------------------------------------------------------- 1 | #ifndef LightDeviceDriver_H 2 | #define LightDeviceDriver_H 3 | 4 | typedef struct LightDeviceDriver LightDeviceDriver; 5 | struct LightDeviceDriver 6 | { 7 | 8 | }; 9 | 10 | /* Constructors and destructors:*/ 11 | void LightDeviceDriver_Init(LightDeviceDriver* const me); 12 | void LightDeviceDriver_Cleanup(LightDeviceDriver* const me); 13 | 14 | /* Operations */ 15 | 16 | LightDeviceDriver * LightDeviceDriver_Create(void); 17 | void LightDeviceDriver_Destroy(LightDeviceDriver* const me); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /chapter2/02-observer/TimeMarkedData.h: -------------------------------------------------------------------------------- 1 | #ifndef TimeMarkedData_H 2 | #define TimeMarkedData_H 3 | 4 | #include "ECGPkg.h" 5 | 6 | typedef struct TimeMarkedData TimeMarkedData; 7 | struct TimeMarkedData 8 | { 9 | int timeInterval; 10 | int dataValue; 11 | struct TMDQueue* itsTMDQueue; 12 | }; 13 | 14 | void TimeMarkedData_Init(TimeMarkedData* const me); 15 | void TimeMarkedData_Cleanup(TimeMarkedData* const me); 16 | 17 | void TimeMarkedData_setItsTMDQueue(TimeMarkedData* const me, struct TMDQueue* p_TMDQueue); 18 | 19 | #endif //TimeMarkedData_H 20 | -------------------------------------------------------------------------------- /chapter3/05-debounce/Timer.c: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | void Timer_Init(Timer* const me){ 4 | // me-> 5 | } 6 | void Timer_Cleanup(Timer* const me){ 7 | 8 | } 9 | 10 | Timer* Timer_Create(){ 11 | Timer* me = (Timer*)malloc(sizeof(Timer)); 12 | if (me != NULL) 13 | { 14 | Timer_Init(me); 15 | } 16 | return me; 17 | } 18 | 19 | void Timer_Destroy(Timer* const me){ 20 | if (me != NULL) 21 | { 22 | Timer_Cleanup(me); 23 | } 24 | free(me); 25 | } 26 | 27 | void Timer_delay(Timer* const me, int time){ 28 | } 29 | -------------------------------------------------------------------------------- /chapter3/06-interupt/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Button.h" 3 | #include "LED.h" 4 | 5 | int main(int argc, char const *argv[]) 6 | { 7 | int j; 8 | Button* itsButton; 9 | LED* itsLED; 10 | itsButton = Button_Create(); 11 | itsLED = LED_Create(); 12 | 13 | for (j=0;j<9;j++) { 14 | ISRAddress[j] = NULL; 15 | oldVectors[j] = NULL; 16 | }; 17 | ButtonHandler_setItsLED(&itsLED); 18 | install(); /* install interrupt vectors */ 19 | /* normal system execution stuff */ 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /chapter6/01-OwnShipAttitude/AttitudeDataType.h: -------------------------------------------------------------------------------- 1 | #ifndef AttitudeDataType_H 2 | #define AttitudeDataType_H 3 | 4 | #define ATTITUDE_MEMORY_FAULT 0 5 | 6 | typedef struct AttitudeDataType AttitudeDataType; 7 | struct AttitudeDataType 8 | { 9 | int roll; 10 | int yaw; 11 | int pitch; 12 | }; 13 | 14 | void AttitudeDataType_Init(AttitudeDataType* const me); 15 | void AttitudeDataType_Cleanup(AttitudeDataType* const me); 16 | AttitudeDataType* AttitudeDataType_Create(void); 17 | void AttitudeDataType_Destroy(AttitudeDataType* const me); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /chapter2/01-client-server/TimeMarkedData.h: -------------------------------------------------------------------------------- 1 | #ifndef TimeMarkedData_H 2 | #define TimeMarkedData_H 3 | 4 | #include "ECGPkg.h" 5 | 6 | typedef struct TimeMarkedData TimeMarkedData; 7 | struct TimeMarkedData 8 | { 9 | int timeInterval; 10 | int dataValue; 11 | struct TMDQueue* itsTMDQueue; 12 | }; 13 | 14 | void TimeMarkedData_Init(TimeMarkedData* const me); 15 | void TimeMarkedData_Cleanup(TimeMarkedData* const me); 16 | 17 | void TimeMarkedData_setItsTMDQueue(TimeMarkedData* const me, struct TMDQueue* p_TMDQueue); 18 | 19 | #endif //TimeMarkedData_H 20 | -------------------------------------------------------------------------------- /chapter3/03-intermediary/Action.h: -------------------------------------------------------------------------------- 1 | #ifndef Action_H 2 | #define Action_H 3 | 4 | #include 5 | 6 | typedef struct Action Action; 7 | struct Action 8 | { 9 | int rotatingArmJoint1; 10 | int rotatingArmJoint2; 11 | int rotatingArmJoint3; 12 | int rotatingArmJoint4; 13 | int slidingArmJoint1; 14 | int slidingArmJoint2; 15 | int manipulatorForce; 16 | int manipulatorOpen; 17 | }; 18 | 19 | void Action_Init(Action* me); 20 | void Action_Cleanup(Action* me); 21 | 22 | Action* Action_Create(); 23 | void Action_Destroy(Action* me); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /chapter4/05-queue/GasData.c: -------------------------------------------------------------------------------- 1 | #include "GasData.h" 2 | 3 | void GasData_Init(GasData* const me) { 4 | me->conc = 0.0; 5 | me->flowInCCPerMin = 0; 6 | me->gType = UNKNOWN_GAS; 7 | } 8 | 9 | void GasData_Cleanup(GasData* const me) { 10 | } 11 | 12 | GasData * GasData_Create(void) { 13 | GasData* me = (GasData *) malloc(sizeof(GasData)); 14 | if(me!=NULL) { 15 | GasData_Init(me); 16 | } 17 | return me; 18 | } 19 | 20 | void GasData_Destroy(GasData* const me) { 21 | if(me!=NULL) { 22 | GasData_Cleanup(me); 23 | } 24 | free(me); 25 | } 26 | -------------------------------------------------------------------------------- /chapter3/01-hardwareproxy/MotorData.h: -------------------------------------------------------------------------------- 1 | #ifndef MotorData_H 2 | #define MotorData_H 3 | 4 | #include "HWProxyExample.h" 5 | 6 | typedef struct MotorData MotorData; 7 | struct MotorData { 8 | unsigned char on_off; 9 | DirectionType direction; 10 | unsigned int speed; 11 | unsigned char errorStatus; 12 | unsigned char noPowerError; 13 | unsigned char noTorqueError; 14 | unsigned char BITError; 15 | unsigned char overTemperatureError; 16 | unsigned char reservedError1; 17 | unsigned char reservedError2; 18 | unsigned char unknownError; 19 | }; 20 | 21 | #endif -------------------------------------------------------------------------------- /chapter4/05-queue/HeSensor.h: -------------------------------------------------------------------------------- 1 | #ifndef HeSensor_H 2 | #define HeSensor_H 3 | 4 | #include 5 | #include 6 | #include "QueuingExample.h" 7 | 8 | typedef struct HeSensor HeSensor; 9 | struct HeSensor { 10 | double conc; 11 | unsigned int flow; 12 | }; 13 | 14 | /* Constructors and destructors:*/ 15 | void HeSensor_Init(HeSensor* const me); 16 | void HeSensor_Cleanup(HeSensor* const me); 17 | 18 | /* Operations */ 19 | 20 | void HeSensor_getHeData(HeSensor* const me); 21 | HeSensor * HeSensor_Create(void); 22 | void HeSensor_Destroy(HeSensor* const me); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /chapter4/05-queue/N2Sensor.h: -------------------------------------------------------------------------------- 1 | #ifndef N2Sensor_H 2 | #define N2Sensor_H 3 | 4 | #include 5 | #include 6 | #include "QueuingExample.h" 7 | 8 | typedef struct N2Sensor N2Sensor; 9 | struct N2Sensor { 10 | double conc; 11 | unsigned int flow; 12 | }; 13 | 14 | /* Constructors and destructors:*/ 15 | void N2Sensor_Init(N2Sensor* const me); 16 | void N2Sensor_Cleanup(N2Sensor* const me); 17 | 18 | /* Operations */ 19 | 20 | void N2Sensor_getN2Data(N2Sensor* const me); 21 | N2Sensor * N2Sensor_Create(void); 22 | void N2Sensor_Destroy(N2Sensor* const me); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /chapter4/05-queue/O2Sensor.h: -------------------------------------------------------------------------------- 1 | #ifndef O2Sensor_H 2 | #define O2Sensor_H 3 | 4 | #include 5 | #include 6 | #include "QueuingExample.h" 7 | 8 | typedef struct O2Sensor O2Sensor; 9 | struct O2Sensor { 10 | double conc; 11 | unsigned int flow; 12 | }; 13 | 14 | /* Constructors and destructors:*/ 15 | void O2Sensor_Init(O2Sensor* const me); 16 | void O2Sensor_Cleanup(O2Sensor* const me); 17 | 18 | /* Operations */ 19 | 20 | void O2Sensor_getO2Data(O2Sensor* const me); 21 | O2Sensor * O2Sensor_Create(void); 22 | void O2Sensor_Destroy(O2Sensor* const me); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /chapter3/02-adapter/GasDisplay.c: -------------------------------------------------------------------------------- 1 | #include "GasDisplay.h" 2 | 3 | void displayGas(){ 4 | iO2Sensor sensor; 5 | UltimateO2Adapter* ultimateO2Adapter = UltimateO2Adapter_Create(); 6 | AcmeO2Adapter* acmeO2Adapter = AcmeO2Adapter_Create(); 7 | 8 | int o2conc1 = AcmeO2Adapter_gimmeO2Conc(acmeO2Adapter); 9 | int o2flow1 = AcmeO2Adapter_gimmeO2Flow(acmeO2Adapter); 10 | 11 | int o2conc2 = UltimateO2Adapter_gimmeO2Conc(ultimateO2Adapter); 12 | int o2flow2 = UltimateO2Adapter_gimmeO2Flow(ultimateO2Adapter); 13 | 14 | printf("%d %d %d %d\n", o2conc1, o2flow1, o2conc2, o2flow2); 15 | } 16 | -------------------------------------------------------------------------------- /chapter3/02-adapter/GasMixer.c: -------------------------------------------------------------------------------- 1 | #include "GasMixer.h" 2 | 3 | void mixerGas(){ 4 | iO2Sensor sensor; 5 | UltimateO2Adapter* ultimateO2Adapter = UltimateO2Adapter_Create(); 6 | AcmeO2Adapter* acmeO2Adapter = AcmeO2Adapter_Create(); 7 | 8 | int o2conc1 = AcmeO2Adapter_gimmeO2Conc(acmeO2Adapter); 9 | int o2flow1 = AcmeO2Adapter_gimmeO2Flow(acmeO2Adapter); 10 | 11 | int o2conc2 = UltimateO2Adapter_gimmeO2Conc(ultimateO2Adapter); 12 | int o2flow2 = UltimateO2Adapter_gimmeO2Flow(ultimateO2Adapter); 13 | 14 | printf("%d %d %d %d\n", o2conc1, o2flow1, o2conc2, o2flow2); 15 | } 16 | 17 | 18 | -------------------------------------------------------------------------------- /chapter4/04-guard/GuardedCallExample.h: -------------------------------------------------------------------------------- 1 | #ifndef GuardedCallExample_H 2 | #define GuardedCallExample_H 3 | 4 | 5 | struct Attitude; 6 | struct AttitudeController; 7 | struct FlightDataDisplay; 8 | struct GuardTester; 9 | struct GuardedCallBuilder; 10 | struct KinematicData; 11 | struct Navigator; 12 | struct Position; 13 | 14 | /* OS provided types and functions */ 15 | struct OSSemaphore; 16 | struct OSSemaphore* OS_create_semaphore(void); 17 | void OS_destroy_semaphore(struct OSSemaphore* sPtr); 18 | void OS_lock_semaphore(struct OSSemaphore* sPtr); 19 | void OS_release_semaphore(struct OSSemaphore* sPtr); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /chapter6/03-SmartData/SmartDataExample.h: -------------------------------------------------------------------------------- 1 | #ifndef SmartDataExample_H 2 | #define SmartDataExample_H 3 | 4 | 5 | struct AlarmManager; 6 | struct PatientDataClass; 7 | struct SmartColor; 8 | struct SmartDataClient; 9 | struct SmartInt; 10 | 11 | typedef enum ErrorCodeType { 12 | NO_ERRORS, 13 | BELOW_RANGE, 14 | ABOVE_RANGE, 15 | INCONSISTENT_VALUE, 16 | ILLEGAL_USE_OF_NULL_PTR, 17 | INDEX_OUT_OF_RANGE 18 | } ErrorCodeType; 19 | 20 | typedef enum ColorType { 21 | BLACK, 22 | BROWN, 23 | RED, 24 | PINK, 25 | BLUE, 26 | GREEN, 27 | YELLOW, 28 | WHITE 29 | } ColorType; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /chapter5/04-StatePattern/StatePattern.h: -------------------------------------------------------------------------------- 1 | #ifndef StatePattern_H 2 | #define StatePattern_H 3 | 4 | struct Context; 5 | struct GN_ProcessingFractionState; 6 | struct GN_ProcessingWholeState; 7 | struct Mutex; 8 | struct NoNumberState; 9 | struct SPClient; 10 | struct StateActionSet; 11 | struct StatePatternBulder; 12 | 13 | typedef enum TSTATETYPE { 14 | NULL_STATE, 15 | NONUMBER_STATE, 16 | GN_PROCESSINGWHOLEPART_STATE, 17 | GN_PROCESSINGFRACTIONALPART_STATE 18 | } TSTATETYPE; 19 | 20 | typedef int (*ActionPtr0)(void*); 21 | typedef int (*ActionPtr1)(void*, char); 22 | 23 | int digit(char c); /* returns c-‘0’ */ 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /chapter3/05-debounce/MicrowaveEmitter.h: -------------------------------------------------------------------------------- 1 | #ifndef MicrowaveEmitter_H 2 | #define MicrowaveEmitter_H 3 | 4 | #include 5 | #include 6 | 7 | typedef struct MicrowaveEmitter MicrowaveEmitter; 8 | struct MicrowaveEmitter 9 | { 10 | int deviceState; 11 | }; 12 | 13 | void MicrowaveEmitter_Init(MicrowaveEmitter* const me); 14 | void MicrowaveEmitter_Cleanup(MicrowaveEmitter* const me); 15 | 16 | MicrowaveEmitter* MicrowaveEmitter_Create(); 17 | void MicrowaveEmitter_Destroy(MicrowaveEmitter* const me); 18 | 19 | void MicrowaveEmitter_startEmitting(MicrowaveEmitter* const me); 20 | void MicrowaveEmitter_stopEmitting(MicrowaveEmitter* const me); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /chapter3/06-interupt/LED.c: -------------------------------------------------------------------------------- 1 | #include "LED.h" 2 | 3 | void LED_Init(LED* const me){ 4 | me->LEDStatus = LED_OFF; 5 | } 6 | void LED_Cleanup(LED* const me){ 7 | me->LEDStatus = LED_OFF; 8 | } 9 | 10 | LED* LED_Create(){ 11 | LED* me = (LED*)malloc(sizeof(LED)); 12 | if (me != NULL) 13 | { 14 | LED_Init(me); 15 | } 16 | return me; 17 | } 18 | 19 | void LED_Destroy(LED* const me){ 20 | if (me != NULL) 21 | { 22 | LED_Cleanup(me); 23 | } 24 | free(me); 25 | } 26 | 27 | void LED_LightOn(LED* const me){ 28 | me->LEDStatus = LED_ON; 29 | } 30 | 31 | void LED_LightOff(LED* const me){ 32 | me->LEDStatus = LED_OFF; 33 | } 34 | -------------------------------------------------------------------------------- /chapter6/04-channelmode/CalculateColor.h: -------------------------------------------------------------------------------- 1 | #ifndef CalculateColor_H 2 | #define CalculateColor_H 3 | 4 | typedef struct CalculateColor CalculateColor; 5 | struct CalculateColor 6 | { 7 | int red; 8 | int green; 9 | int blue; 10 | }; 11 | 12 | /* Constructors and destructors:*/ 13 | void CalculateColor_Init(CalculateColor* const me); 14 | void CalculateColor_Cleanup(CalculateColor* const me); 15 | 16 | /* Operations */ 17 | 18 | CalculateColor * CalculateColor_Create(void); 19 | void CalculateColor_Destroy(CalculateColor* const me); 20 | 21 | void CalculateColor_setItsLightDeviceDriver(CalculateColor* const me, LightDeviceDriver* p_LightDeviceDriver); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /chapter3/04-observer/GasNotificationHandle.h: -------------------------------------------------------------------------------- 1 | #ifndef GasNotificationHandle_H 2 | #define GasNotificationHandle_H 3 | 4 | #include "GasData.h" 5 | #include 6 | #include 7 | 8 | typedef struct GasNotificationHandle GasNotificationHandle; 9 | struct GasNotificationHandle 10 | { 11 | void (*acceptorPtr)(void *, struct GasData*); 12 | void* instancePtr; 13 | }; 14 | 15 | void GasNotificationHandle_Init(GasNotificationHandle* const me); 16 | void GasNotificationHandle_Cleanup(GasNotificationHandle* const me); 17 | 18 | GasNotificationHandle * GasNotificationHandle_Create(void); 19 | void GasNotificationHandle_Destroy(GasNotificationHandle* const me); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /chapter4/05-queue/SensorThread.h: -------------------------------------------------------------------------------- 1 | #ifndef SensorThread_H 2 | #define SensorThread_H 3 | 4 | #include 5 | #include 6 | #include "O2Sensor.h" 7 | #include "N2Sensor.h" 8 | #include "HeSensor.h" 9 | #include "GasDataQueue.h" 10 | 11 | /* … initial declaratons stuff above … */ 12 | 13 | typedef struct SensorThread SensorThread; 14 | struct SensorThread { 15 | struct GasDataQueue* itsGasDataQueue; 16 | struct HeSensor itsHeSensor; 17 | struct N2Sensor itsN2Sensor; 18 | struct O2Sensor itsO2Sensor; 19 | }; 20 | 21 | 22 | /* Operations */ 23 | 24 | void SensorThread_updateData(SensorThread* const me); 25 | 26 | /* … other operations declared too … */ 27 | #endif 28 | -------------------------------------------------------------------------------- /chapter4/05-queue/GasDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef GasDisplay_H 2 | #define GasDisplay_H 3 | 4 | #include 5 | #include 6 | #include "QueuingExample.h" 7 | #include "GasData.h" 8 | 9 | typedef struct GasDisplay GasDisplay; 10 | struct GasDisplay { 11 | int screenWidth; 12 | }; 13 | 14 | /* Constructors and destructors:*/ 15 | void GasDisplay_Init(GasDisplay* const me); 16 | void GasDisplay_Cleanup(GasDisplay* const me); 17 | 18 | /* Operations */ 19 | void GasDisplay_printGasData(GasDisplay* const me, const GAS_TYPE gasType, double gas_conc, unsigned int gas_flow); 20 | 21 | GasDisplay * GasDisplay_Create(void); 22 | 23 | void GasDisplay_Destroy(GasDisplay* const me); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /chapter5/05-AndState/AndStateList.c: -------------------------------------------------------------------------------- 1 | #include "AndStateList.h" 2 | #include "ErrorStateClass.h" 3 | 4 | 5 | void AndStateList_Init(AndStateList* const me) { 6 | me->nAndStates = 0; 7 | int j; 8 | for (j=0; jandStates[j] = NULL; 10 | } 11 | 12 | void AndStateList_Cleanup(AndStateList* const me) { 13 | } 14 | 15 | AndStateList * AndStateList_Create(void) { 16 | AndStateList* me = (AndStateList *) malloc(sizeof(AndStateList)); 17 | if(me!=NULL) 18 | AndStateList_Init(me); 19 | return me; 20 | } 21 | 22 | void AndStateList_Destroy(AndStateList* const me) { 23 | if(me!=NULL) 24 | AndStateList_Cleanup(me); 25 | free(me); 26 | } 27 | -------------------------------------------------------------------------------- /chapter6/04-channelmode/ConvertToFrequency.h: -------------------------------------------------------------------------------- 1 | #ifndef ConvertToFrequency_H 2 | #define ConvertToFrequency_H 3 | 4 | typedef struct ConvertToFrequency ConvertToFrequency; 5 | struct ConvertToFrequency 6 | { 7 | 8 | }; 9 | 10 | /* Constructors and destructors:*/ 11 | void ConvertToFrequency_Init(ConvertToFrequency* const me); 12 | void ConvertToFrequency_Cleanup(ConvertToFrequency* const me); 13 | 14 | /* Operations */ 15 | 16 | ConvertToFrequency * ConvertToFrequency_Create(void); 17 | void ConvertToFrequency_Destroy(ConvertToFrequency* const me); 18 | 19 | void ConvertToFrequency_setItsMovingAverageFilter(ConvertToFrequency* const me, MovingAverageFilter* const p_MovingAverageFilter); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /chapter1/01-Sensor/SensorTest.c: -------------------------------------------------------------------------------- 1 | #include "Sensor.h" 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, char const *argv[]) 7 | { 8 | Sensor *p_Sensor0, *p_Sensor1; 9 | p_Sensor0 = Sensor_Create(); 10 | p_Sensor1 = Sensor_Create(); 11 | 12 | // do stuff with the sensors ere 13 | p_Sensor0->value = 99; 14 | p_Sensor1->value = 1; 15 | 16 | printf("The current value from Sensor0 is %d\n", Sensor_getValue(p_Sensor0)); 17 | printf("The current value from Sensor1 is %d\n", Sensor_getValue(p_Sensor1)); 18 | // done with sensors 19 | 20 | Sensor_Destroy(p_Sensor0); 21 | Sensor_Destroy(p_Sensor1); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /chapter5/04-StatePattern/StateActionSet.h: -------------------------------------------------------------------------------- 1 | #ifndef StateActionSet_H 2 | #define StateActionSet_H 3 | 4 | #include "StatePattern.h" 5 | 6 | 7 | typedef struct StateActionSet StateActionSet; 8 | struct StateActionSet { 9 | ActionPtr0 entryAction; 10 | ActionPtr1 evDigitHandler; 11 | ActionPtr0 evDotHandler; 12 | ActionPtr0 evEOSHandler; 13 | ActionPtr0 evWSHandler; 14 | ActionPtr0 exitAction; 15 | }; 16 | 17 | /* Constructors and destructors:*/ 18 | void StateActionSet_Init(StateActionSet* const me); 19 | void StateActionSet_Cleanup(StateActionSet* const me); 20 | 21 | StateActionSet * StateActionSet_Create(void); 22 | void StateActionSet_Destroy(StateActionSet* const me); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /chapter3/04-observer/GasNotificationHandle.c: -------------------------------------------------------------------------------- 1 | #include "GasNotificationHandle.h" 2 | 3 | void GasNotificationHandle_Init(GasNotificationHandle* const me){ 4 | 5 | } 6 | void GasNotificationHandle_Cleanup(GasNotificationHandle* const me){ 7 | 8 | } 9 | 10 | GasNotificationHandle * GasNotificationHandle_Create(void){ 11 | GasNotificationHandle* me = (GasNotificationHandle*)malloc(sizeof(GasNotificationHandle)); 12 | if (me != NULL) 13 | { 14 | GasNotificationHandle_Init(me); 15 | } 16 | return me; 17 | } 18 | void GasNotificationHandle_Destroy(GasNotificationHandle* const me){ 19 | if (me != NULL) 20 | { 21 | GasNotificationHandle_Cleanup(me); 22 | } 23 | free(me); 24 | } 25 | -------------------------------------------------------------------------------- /chapter6/04-channelmode/MovingAverageFilter.h: -------------------------------------------------------------------------------- 1 | #ifndef MovingAverageFilter_H 2 | #define MovingAverageFilter_H 3 | 4 | typedef struct MovingAverageFilter MovingAverageFilter; 5 | struct MovingAverageFilter 6 | { 7 | int computedFreq; 8 | }; 9 | 10 | /* Constructors and destructors:*/ 11 | void MovingAverageFilter_Init(MovingAverageFilter* const me); 12 | void MovingAverageFilter_Cleanup(MovingAverageFilter* const me); 13 | 14 | /* Operations */ 15 | 16 | MovingAverageFilter * MovingAverageFilter_Create(void); 17 | void MovingAverageFilter_Destroy(MovingAverageFilter* const me); 18 | 19 | void MovingAverageFilter_setItsCalculateColor(MovingAverageFilter* const me, CalculateColor* const p_CalculateColor); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /chapter3/05-debounce/Button.h: -------------------------------------------------------------------------------- 1 | #ifndef BUTTON_H 2 | #define BUTTON_H 3 | 4 | #include "ButtonDriver.h" 5 | #include 6 | #include 7 | 8 | typedef struct Button Button; 9 | struct Button 10 | { 11 | int deviceState; 12 | int backlight; 13 | ButtonDriver* buttonDriver; 14 | }; 15 | 16 | void Button_Init(Button* const me); 17 | void Button_Cleanup(Button* const me); 18 | 19 | Button* Button_Create(); 20 | void Button_Destroy(Button* const me); 21 | 22 | void Button_backlight(Button* const me, int light); 23 | int Button_getState(Button* const me); 24 | void Button_setItsButtonDriver(Button* const me, ButtonDriver* const buttonDriver); 25 | ButtonDriver* Button_getItsButtonDriver(Button* const me); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /chapter3/02-adapter/AcmeO2Adapter.h: -------------------------------------------------------------------------------- 1 | #ifndef AcmeO2Adapter_H 2 | #define AcmeO2Adapter_H 3 | 4 | #include "AcmeO2SensorProxy.h" 5 | #include "iO2Sensor.h" 6 | #include 7 | #include 8 | 9 | typedef struct AcmeO2Adapter AcmeO2Adapter; 10 | struct AcmeO2Adapter 11 | { 12 | iO2Sensor* iO2Sensor; 13 | AcmeO2SensorProxy* itsAcmeO2SensorProxy; 14 | }; 15 | 16 | void AcmeO2Adapter_Init(AcmeO2Adapter* const me); 17 | void AcmeO2Adapter_Cleanup(AcmeO2Adapter* const me); 18 | 19 | AcmeO2Adapter* AcmeO2Adapter_Create(); 20 | void AcmeO2Adapter_Destroy(AcmeO2Adapter* me); 21 | 22 | int AcmeO2Adapter_gimmeO2Conc(AcmeO2Adapter* const me); 23 | int AcmeO2Adapter_gimmeO2Flow(AcmeO2Adapter* const me); 24 | 25 | #endif //AcmeO2Adapter_H 26 | -------------------------------------------------------------------------------- /chapter4/07-simulaLock/OpticalSpeedSensor.c: -------------------------------------------------------------------------------- 1 | #include "OpticalSpeedSensor.h" 2 | 3 | void OpticalSpeedSensor_Init(OpticalSpeedSensor* const me){ 4 | 5 | } 6 | void OpticalSpeedSensor_Cleanup(OpticalSpeedSensor* const me){ 7 | 8 | } 9 | OpticalSpeedSensor* OpticalSpeedSensor_Create(void){ 10 | 11 | } 12 | void OpticalSpeedSensor_Destroy(OpticalSpeedSensor* const me){ 13 | 14 | } 15 | 16 | void OpticalSpeedSensor_configure(OpticalSpeedSensor* const me, int wheelSize, int sensitivity){ 17 | 18 | } 19 | 20 | void OpticalSpeedSensor_disable(OpticalSpeedSensor* const me){ 21 | 22 | } 23 | 24 | void OpticalSpeedSensor_enable(OpticalSpeedSensor* const me){ 25 | 26 | } 27 | 28 | int OpticalSpeedSensor_getSpeed(OpticalSpeedSensor* const me){ 29 | 30 | } 31 | -------------------------------------------------------------------------------- /chapter2/01-client-server/WaveformDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef WaveformDisplay_H 2 | #define WaveformDisplay_H 3 | 4 | #include "ECGPkg.h" 5 | 6 | struct TMDQueue; 7 | 8 | typedef struct WaveformDisplay WaveformDisplay; 9 | struct WaveformDisplay 10 | { 11 | int index; 12 | struct TMDQueue* itsTMDQueue; 13 | }; 14 | 15 | void WaveformDisplay_Init(WaveformDisplay* const me); 16 | void WaveformDisplay_Cleanup(WaveformDisplay* const me); 17 | 18 | void WaveformDisplay_getScalarValue(WaveformDisplay* const me); 19 | void WaveformDisplay_setItsTMDQueue(WaveformDisplay* const me, struct TMDQueue* p_TMDQueue); 20 | 21 | WaveformDisplay* WaveformDisplay_Create(void); 22 | WaveformDisplay* WaveformDisplay_Destroy(WaveformDisplay* const me); 23 | 24 | #endif //WaveformDisplay_H 25 | -------------------------------------------------------------------------------- /chapter3/07-poll/BreathingCircuitSensor.h: -------------------------------------------------------------------------------- 1 | #ifndef BreathingCircuitSensor_H 2 | #define BreathingCircuitSensor_H 3 | 4 | #include 5 | #include 6 | 7 | typedef struct BreathingCircuitSensor BreathingCircuitSensor; 8 | struct BreathingCircuitSensor 9 | { 10 | int data; 11 | int state; 12 | }; 13 | 14 | void BreathingCircuitSensor_Init(BreathingCircuitSensor* const me); 15 | void BreathingCircuitSensor_Cleanup(BreathingCircuitSensor* const me); 16 | 17 | BreathingCircuitSensor* BreathingCircuitSensor_Create(); 18 | void BreathingCircuitSensor_Destroy(BreathingCircuitSensor* me); 19 | 20 | int BreathingCircuitSensor_getData(BreathingCircuitSensor* const me); 21 | int BreathingCircuitSensor_getState(BreathingCircuitSensor* const me); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /chapter5/04-StatePattern/StateActionSet.c: -------------------------------------------------------------------------------- 1 | #include "StateActionSet.h" 2 | 3 | void StateActionSet_Init(StateActionSet* const me) { 4 | me->entryAction = NULL; 5 | me->evDigitHandler = NULL; 6 | me->evDotHandler = NULL; 7 | me->evEOSHandler = NULL; 8 | me->evWSHandler = NULL; 9 | me->exitAction = NULL; 10 | } 11 | 12 | void StateActionSet_Cleanup(StateActionSet* const me) { 13 | } 14 | 15 | StateActionSet * StateActionSet_Create(void) { 16 | StateActionSet* me = (StateActionSet *) malloc(sizeof(StateActionSet)); 17 | if(me!=NULL) 18 | StateActionSet_Init(me); 19 | return me; 20 | } 21 | 22 | void StateActionSet_Destroy(StateActionSet* const me) { 23 | if(me!=NULL) 24 | StateActionSet_Cleanup(me); 25 | free(me); 26 | } 27 | -------------------------------------------------------------------------------- /chapter3/07-poll/MedicalDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef MedicalDisplay_H 2 | #define MedicalDisplay_H 3 | 4 | #include 5 | #include 6 | 7 | typedef struct MedicalDisplay MedicalDisplay; 8 | struct MedicalDisplay 9 | { 10 | 11 | }; 12 | 13 | void MedicalDisplay_Init(MedicalDisplay* const me); 14 | void MedicalDisplay_Cleanup(MedicalDisplay* const me); 15 | 16 | MedicalDisplay* MedicalDisplay_Create(); 17 | void MedicalDisplay_Destroy(MedicalDisplay* me); 18 | 19 | void MedicalDisplay_showO2Concentration(MedicalDisplay* const me, int data); 20 | void MedicalDisplay_showGasFlow(MedicalDisplay* const me, int data); 21 | void MedicalDisplay_showGasFlowStatus(MedicalDisplay* const me, int state); 22 | void MedicalDisplay_showCircuitPressure(MedicalDisplay* const me, int data); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /chapter3/02-adapter/UltimateO2Adapter.h: -------------------------------------------------------------------------------- 1 | #ifndef UltimateO2Adapter_H 2 | #define UltimateO2Adapter_H 3 | 4 | #include "UltimateO2SensorProxy.h" 5 | #include "iO2Sensor.h" 6 | #include 7 | #include 8 | 9 | typedef struct UltimateO2Adapter UltimateO2Adapter; 10 | struct UltimateO2Adapter 11 | { 12 | iO2Sensor* iO2Sensor; 13 | UltimateO2SensorProxy* itsUltimateO2SensorProxy; 14 | }; 15 | 16 | void UltimateO2Adapter_Init(UltimateO2Adapter* const me); 17 | void UltimateO2Adapter_Cleanup(UltimateO2Adapter* const me); 18 | 19 | int UltimateO2Adapter_gimmeO2Conc(UltimateO2Adapter* const me); 20 | int UltimateO2Adapter_gimmeO2Flow(UltimateO2Adapter* const me); 21 | 22 | UltimateO2Adapter* UltimateO2Adapter_Create(); 23 | void UltimateO2Adapter_Destroy(UltimateO2Adapter* me); 24 | 25 | #endif //UltimateO2Adapter 26 | -------------------------------------------------------------------------------- /chapter5/05-AndState/StateActionSet.h: -------------------------------------------------------------------------------- 1 | #ifndef StateActionSet_H 2 | #define StateActionSet_H 3 | 4 | #include "LightPkg.h" 5 | 6 | typedef struct StateActionSet StateActionSet; 7 | struct StateActionSet { 8 | ActionPtr0 entryAction; 9 | ActionPtr0 evAbortHandler; 10 | ActionPtr0 evDisableHandler; 11 | ActionPtr0 evEnableHandler; 12 | ActionPtr0 evErrorHandler; 13 | ActionPtr0 evOkHandler; 14 | ActionPtr0 evRunHandler; 15 | ActionPtr0 evShutDownHandler; 16 | ActionPtr0 evWarningHandler; 17 | ActionPtr0 exitAction; 18 | ActionPtr1 is_inHandler; 19 | }; 20 | 21 | 22 | void StateActionSet_Init(StateActionSet* const me); 23 | void StateActionSet_Cleanup(StateActionSet* const me); 24 | StateActionSet * StateActionSet_Create(void); 25 | void StateActionSet_Destroy(StateActionSet* const me); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /chapter4/01-loop/loop2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | here is where private static data goes 5 | */ 6 | static int taskAInvocationCounter=0; 7 | 8 | void taskA(void) { 9 | /* more private task data */ 10 | int stufftoDo; 11 | 12 | /* initialization code */ 13 | stuffToDo = 1; 14 | while (stuffToDo) { 15 | signal = waitOnSignal(); 16 | switch (signal) { 17 | case signal1: 18 | /* signal 1 processing here */ 19 | break; 20 | case signal2: 21 | /* signal 2 processing here */ 22 | case signal3: 23 | /* signal 3 processing here */ 24 | }; 25 | }; /* end infinite while loop */ 26 | }; /* end task */ 27 | 28 | int main(int argc, char const *argv[]) 29 | { 30 | taskA(); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /chapter4/07-simulaLock/DopplerSpeedSensor.h: -------------------------------------------------------------------------------- 1 | #ifndef DopplerSpeedSensor_H 2 | #define DopplerSpeedSensor_H 3 | 4 | typedef struct DopplerSpeedSensor DopplerSpeedSensor; 5 | struct DopplerSpeedSensor 6 | { 7 | int sampleRate; 8 | int state; 9 | int speed; 10 | }; 11 | 12 | void DopplerSpeedSensor_Init(DopplerSpeedSensor* const me); 13 | void DopplerSpeedSensor_Cleanup(DopplerSpeedSensor* const me); 14 | DopplerSpeedSensor* DopplerSpeedSensor_Create(void); 15 | void DopplerSpeedSensor_Destroy(DopplerSpeedSensor* const me); 16 | 17 | void DopplerSpeedSensor_configure(DopplerSpeedSensor* const me, int sampleRate); 18 | 19 | int DopplerSpeedSensor_getSpeed(DopplerSpeedSensor* const me); 20 | 21 | void DopplerSpeedSensor_enable(DopplerSpeedSensor* const me); 22 | void DopplerSpeedSensor_disable(DopplerSpeedSensor* const me); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /chapter4/05-queue/HeSensor.c: -------------------------------------------------------------------------------- 1 | #include "N2Sensor.h" 2 | #include 3 | #include 4 | 5 | void N2Sensor_Init(N2Sensor* const me) { 6 | me->conc = 0.0; 7 | me->flow = 0; 8 | } 9 | 10 | void N2Sensor_Cleanup(N2Sensor* const me) { 11 | } 12 | 13 | /* 14 | getO2Data() is where the sensor class would 15 | actually acquire the data. Here it just 16 | augments it 17 | */ 18 | void N2Sensor_getN2Data(N2Sensor* const me) { 19 | me->conc += 20; 20 | me->flow += 25; 21 | } 22 | 23 | N2Sensor * N2Sensor_Create(void) { 24 | N2Sensor* me = (N2Sensor *) malloc(sizeof(N2Sensor)); 25 | if(me!=NULL) { 26 | N2Sensor_Init(me); 27 | } 28 | return me; 29 | } 30 | 31 | void N2Sensor_Destroy(N2Sensor* const me) { 32 | if(me!=NULL) { 33 | N2Sensor_Cleanup(me); 34 | } 35 | free(me); 36 | } 37 | -------------------------------------------------------------------------------- /chapter4/05-queue/N2Sensor.c: -------------------------------------------------------------------------------- 1 | #include "HeSensor.h" 2 | #include 3 | #include 4 | 5 | void HeSensor_Init(HeSensor* const me) { 6 | me->conc = 0.0; 7 | me->flow = 0; 8 | } 9 | 10 | void HeSensor_Cleanup(HeSensor* const me) { 11 | } 12 | 13 | /* 14 | getO2Data() is where the sensor class would 15 | actually acquire the data. Here it just 16 | augments it 17 | */ 18 | void HeSensor_getHeData(HeSensor* const me) { 19 | me->conc += 20; 20 | me->flow += 25; 21 | } 22 | 23 | HeSensor * HeSensor_Create(void) { 24 | HeSensor* me = (HeSensor *) malloc(sizeof(HeSensor)); 25 | if(me!=NULL) { 26 | HeSensor_Init(me); 27 | } 28 | return me; 29 | } 30 | 31 | void HeSensor_Destroy(HeSensor* const me) { 32 | if(me!=NULL) { 33 | HeSensor_Cleanup(me); 34 | } 35 | free(me); 36 | } 37 | -------------------------------------------------------------------------------- /chapter4/05-queue/O2Sensor.c: -------------------------------------------------------------------------------- 1 | #include "O2Sensor.h" 2 | #include 3 | #include 4 | 5 | void O2Sensor_Init(O2Sensor* const me) { 6 | me->conc = 0.0; 7 | me->flow = 0; 8 | } 9 | 10 | void O2Sensor_Cleanup(O2Sensor* const me) { 11 | } 12 | 13 | /* 14 | getO2Data() is where the sensor class would 15 | actually acquire the data. Here it just 16 | augments it 17 | */ 18 | void O2Sensor_getO2Data(O2Sensor* const me) { 19 | me->conc += 20; 20 | me->flow += 25; 21 | } 22 | 23 | O2Sensor * O2Sensor_Create(void) { 24 | O2Sensor* me = (O2Sensor *) malloc(sizeof(O2Sensor)); 25 | if(me!=NULL) { 26 | O2Sensor_Init(me); 27 | } 28 | return me; 29 | } 30 | 31 | void O2Sensor_Destroy(O2Sensor* const me) { 32 | if(me!=NULL) { 33 | O2Sensor_Cleanup(me); 34 | } 35 | free(me); 36 | } 37 | -------------------------------------------------------------------------------- /chapter4/05-queue/QueuingExample.h: -------------------------------------------------------------------------------- 1 | #ifndef QueuingExample_H 2 | #define QueuingExample_H 3 | 4 | #include 5 | #include 6 | 7 | struct GasController; 8 | struct GasData; 9 | struct GasDataQueue; 10 | struct GasDisplay; 11 | struct GasProcessingThread; 12 | struct HeSensor; 13 | struct N2Sensor; 14 | struct O2Sensor; 15 | struct OSSemaphore; 16 | struct SensorThread; 17 | 18 | typedef enum GAS_TYPE { 19 | O2_GAS, 20 | N2_GAS, 21 | HE_GAS, 22 | UNKNOWN_GAS 23 | } GAS_TYPE; 24 | 25 | /* define the size of the queue */ 26 | #define GAS_QUEUE_SIZE (10) 27 | 28 | /* OS semaphore services */ 29 | struct OSSemaphore* OS_create_semaphore(void); 30 | void OS_destroy_semaphore(struct OSSemaphore* sPtr); 31 | void OS_lock_semaphore(struct OSSemaphore* sPtr); 32 | void OS_release_semaphore(struct OSSemaphore* sPtr); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /chapter4/07-simulaLock/OpticalSpeedSensor.h: -------------------------------------------------------------------------------- 1 | #ifndef OpticalSpeedSensor_H 2 | #define OpticalSpeedSensor_H 3 | 4 | typedef struct OpticalSpeedSensor OpticalSpeedSensor; 5 | struct OpticalSpeedSensor 6 | { 7 | int wheelSize; 8 | int sensitivity; 9 | int speed; 10 | }; 11 | 12 | void OpticalSpeedSensor_Init(OpticalSpeedSensor* const me); 13 | void OpticalSpeedSensor_Cleanup(OpticalSpeedSensor* const me); 14 | OpticalSpeedSensor* OpticalSpeedSensor_Create(void); 15 | void OpticalSpeedSensor_Destroy(OpticalSpeedSensor* const me); 16 | 17 | void OpticalSpeedSensor_configure(OpticalSpeedSensor* const me, int wheelSize, int sensitivity); 18 | void OpticalSpeedSensor_disable(OpticalSpeedSensor* const me); 19 | void OpticalSpeedSensor_enable(OpticalSpeedSensor* const me); 20 | int OpticalSpeedSensor_getSpeed(OpticalSpeedSensor* const me); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /chapter4/07-simulaLock/GPSPositionSensor.h: -------------------------------------------------------------------------------- 1 | #ifndef GPSPositionSensor_H 2 | #define GPSPositionSensor_H 3 | 4 | typedef struct GPSPositionSensor GPSPositionSensor; 5 | struct GPSPositionSensor 6 | { 7 | int reqSatellites; 8 | int useFast; 9 | int position; 10 | }; 11 | 12 | void GPSPositionSensor_Init(GPSPositionSensor* const me); 13 | void GPSPositionSensor_Cleanup(GPSPositionSensor* const me); 14 | 15 | GPSPositionSensor* GPSPositionSensor_Create(void); 16 | void GPSPositionSensor_Destroy(GPSPositionSensor* const me); 17 | 18 | void GPSPositionSensor_configure(GPSPositionSensor* const me, int reqSatellites, int useFast); 19 | 20 | void GPSPositionSensor_activate(GPSPositionSensor* const me); 21 | void GPSPositionSensor_deactivate(GPSPositionSensor* const me); 22 | 23 | int GPSPositionSensor_getPosition(GPSPositionSensor* const me); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /chapter4/04-guard/Navigator.h: -------------------------------------------------------------------------------- 1 | #ifndef Navigator_H 2 | #define Navigator_H 3 | 4 | #include "GuardedCallExample.h" 5 | #include "Position.h" 6 | struct KinematicData; 7 | 8 | typedef struct Navigator Navigator; 9 | struct Navigator { 10 | struct Position ownShipPosition; 11 | struct KinematicData* itsKinematicData; 12 | }; 13 | 14 | /* Constructors and destructors:*/ 15 | 16 | void Navigator_Init(Navigator* const me); 17 | void Navigator_Cleanup(Navigator* const me); 18 | 19 | /* Operations */ 20 | void Navigator_updatePosition(Navigator* const me); 21 | 22 | struct KinematicData* Navigator_getItsKinematicData(const Navigator* const me); 23 | 24 | void Navigator_setItsKinematicData(Navigator* const me, struct KinematicData* p_KinematicData); 25 | 26 | Navigator * Navigator_Create(void); 27 | 28 | void Navigator_Destroy(Navigator* const me); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /chapter3/01-hardwareproxy/bad/bitfield.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | typedef struct _statusBits { 6 | unsigned enable : 1; 7 | unsigned errorStatus : 1; 8 | unsigned motorSpeed : 4; 9 | unsigned LEDColor : 2; 10 | } statusBits; 11 | 12 | statusBits status; 13 | printf("size = %d\n",(int)sizeof(status)); 14 | 15 | status.enable = 1; 16 | status.errorStatus = 0; 17 | status.motorSpeed = 3; 18 | status.LEDColor = 2; 19 | 20 | if (status.enable) 21 | printf("Enabled\n"); 22 | else 23 | printf("Disabled\n"); 24 | 25 | if (status.errorStatus) 26 | printf("ERROR!\n"); 27 | else 28 | printf("No error\n"); 29 | 30 | printf("Motor speed %d\n",status.motorSpeed); 31 | printf("Color %d\n",status.LEDColor); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /chapter3/07-poll/BCTimer.h: -------------------------------------------------------------------------------- 1 | #ifndef BCTimer_H 2 | #define BCTimer_H 3 | 4 | #include "BCPeriodicPoller.h" 5 | #include 6 | #include 7 | 8 | struct BCPeriodicPoller; 9 | 10 | typedef struct BCTimer BCTimer; 11 | struct BCTimer 12 | { 13 | BCPeriodicPoller* poller; 14 | }; 15 | 16 | void BCTimer_Init(BCTimer* const me); 17 | void BCTimer_Cleanup(BCTimer* const me); 18 | 19 | BCTimer* BCTimer_Create(); 20 | void BCTimer_Destroy(BCTimer* me); 21 | 22 | void BCTimer_startTimer(BCTimer* const me, int pollTime); 23 | void BCTimer_stopTimer(BCTimer* const me); 24 | 25 | void BCTimer_installInterruptHandler(BCTimer* const me); 26 | void BCTimer_removeInterruptHandler(BCTimer* const me); 27 | 28 | void BCTimer_setItsBCPeriodicPoller(BCTimer* const me, BCPeriodicPoller* const poller); 29 | BCPeriodicPoller* BCTimer_getItsBCPeriodicPoller(BCTimer* const me); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /chapter3/07-poll/BreathingCircuitSensor.c: -------------------------------------------------------------------------------- 1 | #include "BreathingCircuitSensor.h" 2 | 3 | void BreathingCircuitSensor_Init(BreathingCircuitSensor* const me){ 4 | 5 | } 6 | void BreathingCircuitSensor_Cleanup(BreathingCircuitSensor* const me){ 7 | 8 | } 9 | 10 | BreathingCircuitSensor* BreathingCircuitSensor_Create(){ 11 | BreathingCircuitSensor* me = (BreathingCircuitSensor*)malloc(sizeof(BreathingCircuitSensor)); 12 | if (me != NULL) 13 | { 14 | BreathingCircuitSensor_Init(me); 15 | } 16 | return me; 17 | } 18 | 19 | void BreathingCircuitSensor_Destroy(BreathingCircuitSensor* me){ 20 | if (me != NULL) 21 | { 22 | BreathingCircuitSensor_Cleanup(me); 23 | } 24 | free(me); 25 | } 26 | 27 | int BreathingCircuitSensor_getData(BreathingCircuitSensor* const me){ 28 | 29 | } 30 | 31 | int BreathingCircuitSensor_getState(BreathingCircuitSensor* const me){ 32 | 33 | } 34 | -------------------------------------------------------------------------------- /chapter2/01-client-server/WaveformDisplay.c: -------------------------------------------------------------------------------- 1 | #include "WaveformDisplay.h" 2 | #include "TMDQueue.h" 3 | 4 | void WaveformDisplay_Init(WaveformDisplay* const me){ 5 | 6 | } 7 | 8 | void WaveformDisplay_Cleanup(WaveformDisplay* const me){ 9 | 10 | } 11 | 12 | void WaveformDisplay_getScalarValue(WaveformDisplay* const me){ 13 | TimeMarkedData tmd; 14 | tmd = TMDQueue_remove(me->itsTMDQueue, me->index); 15 | printf("WaveformDisplay index: %d TimeInterval: %d DataValue: %d\n", me->index, tmd.timeInterval, tmd.dataValue); 16 | me->index = TMDQueue_getNextIndex(me->itsTMDQueue, me->index); 17 | } 18 | 19 | void WaveformDisplay_setItsTMDQueue(WaveformDisplay* const me, struct TMDQueue* p_TMDQueue){ 20 | me->itsTMDQueue = p_TMDQueue; 21 | } 22 | 23 | WaveformDisplay* WaveformDisplay_Create(void){ 24 | 25 | } 26 | 27 | WaveformDisplay* WaveformDisplay_Destroy(WaveformDisplay* const me){ 28 | 29 | } 30 | -------------------------------------------------------------------------------- /chapter2/02-observer/ECG_Module.h: -------------------------------------------------------------------------------- 1 | #ifndef ECG_Module_H 2 | #define ECG_Module_H 3 | 4 | #include "ECGPkg.h" 5 | 6 | struct TMDQueue; 7 | 8 | /* class ECG_Module */ 9 | typedef struct ECG_Module ECG_Module; 10 | struct ECG_Module { 11 | int dataNum; 12 | int lead1; 13 | int lead2; 14 | struct TMDQueue* itsTMDQueue; 15 | }; 16 | 17 | /* Constructors and destructors:*/ 18 | void ECG_Module_Init(ECG_Module* const me); 19 | void ECG_Module_Cleanup(ECG_Module* const me); 20 | 21 | /* Operations */ 22 | void ECG_Module_acquireValue(ECG_Module* const me); 23 | void ECG_Module_setLeadPair(ECG_Module* const me, int l1, int l2); 24 | struct TMDQueue* ECG_Module_getItsTMDQueue(const ECG_Module* const me); 25 | void ECG_Module_setItsTMDQueue(ECG_Module* const me, struct TMDQueue* p_TMDQueue); 26 | 27 | ECG_Module * ECG_Module_Create(void); 28 | void ECG_Module_Destroy(ECG_Module* const me); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /chapter2/02-observer/NotificationHandle.h: -------------------------------------------------------------------------------- 1 | #ifndef NotificationHandle_H 2 | #define NotificationHandle_H 3 | 4 | #include 5 | #include "ECGPkg.h" 6 | 7 | typedef struct NotificationHandle NotificationHandle; 8 | struct NotificationHandle { 9 | UpdateFuncPtr updateAddr; 10 | struct NotificationHandle* itsNotificationHandle; 11 | }; 12 | 13 | /* Constructors and destructors:*/ 14 | void NotificationHandle_Init(NotificationHandle* const me); 15 | void NotificationHandle_Cleanup(NotificationHandle* const me); 16 | 17 | struct NotificationHandle* NotificationHandle_getItsNotificationHandle(const NotificationHandle* const me); 18 | void NotificationHandle_setItsNotificationHandle(NotificationHandle* const me, struct NotificationHandle* p_NotificationHandle); 19 | 20 | NotificationHandle * NotificationHandle_Create(void); 21 | void NotificationHandle_Destroy(NotificationHandle* const me); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /chapter2/01-client-server/ECG_Module.h: -------------------------------------------------------------------------------- 1 | #ifndef ECG_Module_H 2 | #define ECG_Module_H 3 | 4 | #include "ECGPkg.h" 5 | 6 | struct TMDQueue; 7 | 8 | /* class ECG_Module */ 9 | typedef struct ECG_Module ECG_Module; 10 | struct ECG_Module { 11 | int dataNum; 12 | int lead1; 13 | int lead2; 14 | struct TMDQueue* itsTMDQueue; 15 | }; 16 | 17 | /* Constructors and destructors:*/ 18 | void ECG_Module_Init(ECG_Module* const me); 19 | void ECG_Module_Cleanup(ECG_Module* const me); 20 | 21 | /* Operations */ 22 | void ECG_Module_acquireValue(ECG_Module* const me); 23 | void ECG_Module_setLeadPair(ECG_Module* const me, int l1, int l2); 24 | struct TMDQueue* ECG_Module_getItsTMDQueue(const ECG_Module* const me); 25 | void ECG_Module_setItsTMDQueue(ECG_Module* const me, struct TMDQueue* p_TMDQueue); 26 | 27 | ECG_Module * ECG_Module_Create(void); 28 | void ECG_Module_Destroy(ECG_Module* const me); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /chapter3/06-interupt/ButtonHandler.c: -------------------------------------------------------------------------------- 1 | #include "ButtonHandler.h" 2 | #include "LED.h" 3 | #include "RobotInterruptVectorTable.h" 4 | 5 | ButtonVectorPtr oldVectors[10]; 6 | 7 | static struct LED* itsLED; 8 | 9 | void deinstall(void) { 10 | ISRAddress[0] = oldVectors[0]; 11 | ISRAddress[1] = oldVectors[1]; 12 | } 13 | 14 | interrupt void handleButtonPushInterrupt(void) { 15 | LED_LightOn(itsLED); 16 | } 17 | 18 | interrupt void handleButtonReleaseInterrupt(void) { 19 | LED_LightOff(itsLED); 20 | } 21 | 22 | void install(void) { 23 | oldVectors[0] = ISRAddress[0]; 24 | oldVectors[1] = ISRAddress[1]; 25 | ISRAddress[0] = handleButtonPushInterrupt; 26 | ISRAddress[1] = handleButtonReleaseInterrupt; 27 | } 28 | 29 | struct LED* ButtonHandler_getItsLED(void) { 30 | return (struct LED*)itsLED; 31 | } 32 | 33 | void ButtonHandler_setItsLED(struct LED* p_LED) { 34 | itsLED = p_LED; 35 | } 36 | -------------------------------------------------------------------------------- /chapter3/05-debounce/MicrowaveEmitter.c: -------------------------------------------------------------------------------- 1 | #include "MicrowaveEmitter.h" 2 | 3 | void MicrowaveEmitter_Init(MicrowaveEmitter* const me){ 4 | // me-> 5 | } 6 | void MicrowaveEmitter_Cleanup(MicrowaveEmitter* const me){ 7 | 8 | } 9 | 10 | MicrowaveEmitter* MicrowaveEmitter_Create(){ 11 | MicrowaveEmitter* me = (MicrowaveEmitter*)malloc(sizeof(MicrowaveEmitter)); 12 | if (me != NULL) 13 | { 14 | MicrowaveEmitter_Init(me); 15 | } 16 | return me; 17 | } 18 | 19 | void MicrowaveEmitter_Destroy(MicrowaveEmitter* const me){ 20 | if (me != NULL) 21 | { 22 | MicrowaveEmitter_Cleanup(me); 23 | } 24 | free(me); 25 | } 26 | 27 | void MicrowaveEmitter_startEmitting(MicrowaveEmitter* const me){ 28 | printf("call MicrowaveEmitter_startEmitting\n"); 29 | } 30 | void MicrowaveEmitter_stopEmitting(MicrowaveEmitter* const me){ 31 | printf("call MicrowaveEmitter_stopEmitting\n"); 32 | } 33 | -------------------------------------------------------------------------------- /chapter4/04-guard/Attitude.h: -------------------------------------------------------------------------------- 1 | #ifndef Attitude_H 2 | #define Attitude_H 3 | 4 | #include "GuardedCallExample.h" 5 | 6 | typedef struct Attitude Attitude; 7 | struct Attitude { 8 | double pitch; /*## attribute pitch */ 9 | double roll; /*## attribute roll */ 10 | double yaw; /*## attribute yaw */ 11 | }; 12 | 13 | 14 | /* Constructors and destructors:*/ 15 | void Attitude_Init(Attitude* const me); 16 | void Attitude_Cleanup(Attitude* const me); 17 | 18 | /* Operations */ 19 | double Attitude_getPitch(Attitude* const me); 20 | double Attitude_getRoll(Attitude* const me); 21 | double Attitude_getYaw(Attitude* const me); 22 | void Attitude_setPitch(Attitude* const me, double x); 23 | void Attitude_setRoll(Attitude* const me, double x); 24 | void Attitude_setYaw(Attitude* const me, double x); 25 | 26 | Attitude * Attitude_Create(void); 27 | void Attitude_Destroy(Attitude* const me); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /chapter3/04-observer/main.c: -------------------------------------------------------------------------------- 1 | #include "DisplayClient.h" 2 | #include 3 | 4 | int main(int argc, char const *argv[]) 5 | { 6 | DisplayClient* p_DisplayClient = DisplayClient_Create(); 7 | DisplayClient_register(p_DisplayClient); 8 | 9 | // DisplayClient_setItsGasSensor 10 | // DisplayClient_getItsGasSensor 11 | // DisplayClient_setItsGasData 12 | // DisplayClient_getItsGasData 13 | 14 | DisplayClient_alarm(p_DisplayClient, "fuck"); 15 | 16 | unsigned int flow=1; 17 | unsigned int n2=2; 18 | unsigned int o2=3; 19 | 20 | GasSensor* p_GasSensor = (GasSensor*)malloc(sizeof(GasSensor)); 21 | GasSensor_newData(p_GasSensor, flow, n2, o2); 22 | GasSensor_newData(p_GasSensor, flow, n2, o2); 23 | GasSensor_newData(p_GasSensor, flow, n2, o2); 24 | GasSensor_newData(p_GasSensor, flow, n2, o2); 25 | 26 | DisplayClient_Destroy(p_DisplayClient); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /chapter1/02-Queue/QueueTest.c: -------------------------------------------------------------------------------- 1 | #include "Queue.h" 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, char const *argv[]) 7 | { 8 | int j,k,h,t; 9 | 10 | // test normal queue 11 | Queue* myQ; 12 | myQ = Queue_Create(); 13 | k = 1000; 14 | 15 | for (j = 0; j < QUEUE_SIZE; ++j) 16 | { 17 | h = myQ->head; 18 | myQ->insert(myQ, k); 19 | printf("inserting %d at position %d, size=%d\n", k--, h, myQ->getSize(myQ)); 20 | } 21 | printf("inserted %d element\n", myQ->getSize(myQ)); 22 | 23 | for (j = 0; j < QUEUE_SIZE; ++j) 24 | { 25 | t = myQ->tail; 26 | k = myQ->remove(myQ); 27 | printf("removing %d at position %d, size=%d\n", k, t, myQ->getSize(myQ)); 28 | } 29 | printf("last item removed %d\n", k); 30 | printf("current Queue size %d\n", myQ->getSize(myQ)); 31 | 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /chapter6/05-protectsinglechannel/CheckTemperature.h: -------------------------------------------------------------------------------- 1 | #ifndef CheckTemperature_H 2 | #define CheckTemperature_H 3 | 4 | struct FurnaceController; 5 | 6 | typedef struct CheckTemperature CheckTemperature; 7 | struct CheckTemperature { 8 | int maximumTemp; 9 | int minimumTemp; 10 | struct FurnaceController* itsFurnaceController; 11 | }; 12 | 13 | void CheckTemperature_Init(CheckTemperature* const me); 14 | 15 | void CheckTemperature_Cleanup(CheckTemperature* const me); 16 | 17 | void CheckTemperature_checkTemperature(CheckTemperature* const me, int temp); 18 | 19 | struct FurnaceController* CheckTemperature_getItsFurnaceController(const CheckTemperature* const me); 20 | 21 | void CheckTemperature_setItsFurnaceController(CheckTemperature* const me, struct FurnaceController* p_FurnaceController); 22 | 23 | CheckTemperature * CheckTemperature_Create(void); 24 | 25 | void CheckTemperature_Destroy(CheckTemperature* const me); 26 | #endif 27 | -------------------------------------------------------------------------------- /chapter2/01-client-server/QRSDetector.h: -------------------------------------------------------------------------------- 1 | #ifndef QRSDetector_H 2 | #define QRSDetector_H 3 | 4 | #include "ECGPkg.h" 5 | 6 | // https://en.wikipedia.org/wiki/QRS_complex 7 | 8 | struct TMDQueue; 9 | 10 | typedef struct QRSDetector QRSDetector; 11 | struct QRSDetector 12 | { 13 | int heartRate; 14 | int index; 15 | struct TMDQueue* itsTMDQueue; 16 | }; 17 | 18 | // constructions and desctructions 19 | void QRSDetector_Init(QRSDetector* const me); 20 | void QRSDetector_Cleanup(QRSDetector* const me); 21 | 22 | void QRSDetector_computeHR(QRSDetector* const me); 23 | void QRSDetector_getDataSample(QRSDetector* const me); 24 | void QRSDetector_setItsTMDQueue(QRSDetector* const me, struct TMDQueue* p_TMDQueue); 25 | struct TMDQueue* QRSDetector_getItsTMDQueue(const QRSDetector* const me); 26 | 27 | QRSDetector * QRSDetector_Create(void); 28 | QRSDetector * QRSDetector_Destroy(QRSDetector* const me); 29 | 30 | 31 | #endif // QRSDetector_H 32 | -------------------------------------------------------------------------------- /chapter4/05-queue/GasController.h: -------------------------------------------------------------------------------- 1 | #ifndef GasController_H 2 | #define GasController_H 3 | 4 | #include 5 | #include 6 | #include "QueuingExample.h" 7 | #include "GasData.h" 8 | struct GasDisplay; 9 | 10 | typedef struct GasController GasController; 11 | struct GasController { 12 | struct GasDisplay* itsGasDisplay; 13 | }; 14 | 15 | 16 | /* Constructors and destructors:*/ 17 | 18 | void GasController_Init(GasController* const me); 19 | void GasController_Cleanup(GasController* const me); 20 | 21 | /* Operations */ 22 | void GasController_handleGasData(GasController* const me, GasData * gPtr); 23 | 24 | struct GasDisplay* GasController_getItsGasDisplay(const GasController* const me); 25 | 26 | void GasController_setItsGasDisplay(GasController* const me, struct GasDisplay* p_GasDisplay); 27 | 28 | GasController * GasController_Create(void); 29 | 30 | void GasController_Destroy(GasController* const me); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /chapter4/05-queue/GasDataQueue.h: -------------------------------------------------------------------------------- 1 | #ifndef GasDataQueue_H 2 | #define GasDataQueue_H 3 | 4 | #include 5 | #include 6 | #include "QueuingExample.h" 7 | #include "GasData.h" 8 | #include "OSSemaphore.h" 9 | 10 | typedef struct GasDataQueue GasDataQueue; 11 | struct GasDataQueue { 12 | int head; 13 | OSSemaphore * sema; 14 | int size; 15 | int tail; 16 | struct GasData itsGasData[GAS_QUEUE_SIZE]; 17 | }; 18 | 19 | /* Constructors and destructors:*/ 20 | 21 | void GasDataQueue_Init(GasDataQueue* const me); 22 | void GasDataQueue_Cleanup(GasDataQueue* const me); 23 | 24 | /* Operations */ 25 | int GasDataQueue_insert(GasDataQueue* const me, GasData g); 26 | 27 | GasData * GasDataQueue_remove(GasDataQueue* const me); 28 | 29 | int GasDataQueue_getItsGasData(const GasDataQueue* const me); 30 | 31 | GasDataQueue * GasDataQueue_Create(void); 32 | 33 | void GasDataQueue_Destroy(GasDataQueue* const me); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /chapter5/05-AndState/StateActionSet.c: -------------------------------------------------------------------------------- 1 | #include "StateActionSet.h" 2 | 3 | void StateActionSet_Init(StateActionSet* const me) { 4 | me->entryAction = NULL; 5 | me->evAbortHandler = NULL; 6 | me->evDisableHandler = NULL; 7 | me->evEnableHandler = NULL; 8 | me->evErrorHandler = NULL; 9 | me->evOkHandler = NULL; 10 | me->evRunHandler = NULL; 11 | me->evShutDownHandler = NULL; 12 | me->evWarningHandler = NULL; 13 | me->exitAction = NULL; 14 | me->is_inHandler = NULL; 15 | } 16 | 17 | void StateActionSet_Cleanup(StateActionSet* const me) { 18 | } 19 | 20 | StateActionSet * StateActionSet_Create(void) { 21 | StateActionSet* me = (StateActionSet *) malloc(sizeof(StateActionSet)); 22 | if(me!=NULL) 23 | StateActionSet_Init(me); 24 | return me; 25 | } 26 | 27 | void StateActionSet_Destroy(StateActionSet* const me) { 28 | if(me!=NULL) 29 | StateActionSet_Cleanup(me); 30 | free(me); 31 | } 32 | -------------------------------------------------------------------------------- /chapter4/04-guard/FlightDataDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef FlightDataDisplay_H 2 | #define FlightDataDisplay_H 3 | 4 | #include "GuardedCallExample.h" 5 | struct KinematicData; 6 | 7 | typedef struct FlightDataDisplay FlightDataDisplay; 8 | struct FlightDataDisplay { 9 | struct KinematicData* itsKinematicData; 10 | }; 11 | 12 | /* Constructors and destructors:*/ 13 | void FlightDataDisplay_Init(FlightDataDisplay* const me); 14 | void FlightDataDisplay_Cleanup(FlightDataDisplay* const me); 15 | 16 | /* Operations */ 17 | 18 | void FlightDataDisplay_showFlightData(FlightDataDisplay* const me); 19 | 20 | struct KinematicData* FlightDataDisplay_getItsKinematicData(const FlightDataDisplay* const me); 21 | 22 | void FlightDataDisplay_setItsKinematicData(FlightDataDisplay* const me, struct KinematicData* p_KinematicData); 23 | 24 | FlightDataDisplay * FlightDataDisplay_Create(void); 25 | 26 | void FlightDataDisplay_Destroy(FlightDataDisplay* const me); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /chapter6/04-channelmode/EMGSensorDeviceDriver.h: -------------------------------------------------------------------------------- 1 | #ifndef EMGSensorDeviceDriver_H 2 | #define EMGSensorDeviceDriver_H 3 | 4 | typedef struct EMGSensorDeviceDriver EMGSensorDeviceDriver; 5 | struct EMGSensorDeviceDriver 6 | { 7 | int sen; 8 | int voltage; 9 | }; 10 | 11 | /* Constructors and destructors:*/ 12 | void EMGSensorDeviceDriver_Init(EMGSensorDeviceDriver* const me); 13 | void EMGSensorDeviceDriver_Cleanup(EMGSensorDeviceDriver* const me); 14 | 15 | /* Operations */ 16 | 17 | EMGSensorDeviceDriver * EMGSensorDeviceDriver_Create(void); 18 | void EMGSensorDeviceDriver_Destroy(EMGSensorDeviceDriver* const me); 19 | 20 | void EMGSensorDeviceDriver_setSensitivity(EMGSensorDeviceDriver* const me, int sen); 21 | void EMGSensorDeviceDriver_acquireData(EMGSensorDeviceDriver* const me); 22 | 23 | void EMGSensorDeviceDriver_setItsConvertToFrequency(EMGSensorDeviceDriver* const me, ConvertToFrequency* const p_ConvertToFrequency); 24 | 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /chapter3/05-debounce/Button.c: -------------------------------------------------------------------------------- 1 | #include "Button.h" 2 | 3 | void Button_Init(Button* const me){ 4 | me->deviceState = 0; 5 | } 6 | void Button_Cleanup(Button* const me){ 7 | 8 | } 9 | 10 | Button* Button_Create(){ 11 | Button* me = (Button*)malloc(sizeof(Button)); 12 | if (me != NULL) 13 | { 14 | Button_Init(me); 15 | } 16 | return me; 17 | } 18 | 19 | void Button_Destroy(Button* const me){ 20 | if (me != NULL) 21 | { 22 | Button_Cleanup(me); 23 | } 24 | free(me); 25 | } 26 | 27 | void Button_backlight(Button* const me, int backlight){ 28 | me->backlight = backlight; 29 | } 30 | 31 | int Button_getState(Button* const me){ 32 | return me->deviceState; 33 | } 34 | 35 | void Button_setItsButtonDriver(Button* const me, ButtonDriver* const buttonDriver){ 36 | me->buttonDriver = buttonDriver; 37 | } 38 | 39 | ButtonDriver* Button_getItsButtonDriver(Button* const me){ 40 | return me->buttonDriver; 41 | } 42 | -------------------------------------------------------------------------------- /chapter4/04-guard/Position.h: -------------------------------------------------------------------------------- 1 | #ifndef Position_H 2 | #define Position_H 3 | 4 | #include "GuardedCallExample.h" 5 | 6 | typedef struct Position Position; 7 | struct Position { 8 | double altitude; /*## attribute altitude */ 9 | double latitude; /*## attribute latitude */ 10 | double longitude; /*## attribute longitude */ 11 | }; 12 | 13 | /* Constructors and destructors:*/ 14 | void Position_Init(Position* const me); 15 | void Position_Cleanup(Position* const me); 16 | 17 | /* Operations */ 18 | double Position_getAltitude(Position* const me); 19 | double Position_getLatitude(Position* const me); 20 | double Position_getLongitude(Position* const me); 21 | void Position_setAltitude(Position* const me, double x); 22 | void Position_setLatitude(Position* const me, double x); 23 | void Position_setLongitude(Position* const me, double x); 24 | 25 | Position * Position_Create(void); 26 | void Position_Destroy(Position* const me); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /chapter4/06-ThreadBarrier/ThreadBarrier.h: -------------------------------------------------------------------------------- 1 | #ifndef ThreadBarrier_H 2 | #define ThreadBarrier_H 3 | 4 | /*## auto_generated */ 5 | #include 6 | /*## auto_generated */ 7 | #include "RendezvousExample.h" 8 | /*## auto_generated */ 9 | #include 10 | /*## package RendezvousPattern::RendezvousExample */ 11 | 12 | /*## class ThreadBarrier */ 13 | typedef struct ThreadBarrier ThreadBarrier; 14 | struct ThreadBarrier { 15 | int currentCount; 16 | int expectedCount; 17 | OSSemaphore* barrier; 18 | OSSemaphore* mutex; 19 | }; 20 | 21 | /* Constructors and destructors:*/ 22 | void ThreadBarrier_Init(ThreadBarrier* const me); 23 | void ThreadBarrier_Cleanup(ThreadBarrier* const me); 24 | 25 | /* Operations */ 26 | void ThreadBarrier_reset(ThreadBarrier* const me, int x); 27 | void ThreadBarrier_synchronize(ThreadBarrier* const me); 28 | ThreadBarrier * ThreadBarrier_Create(void); 29 | void ThreadBarrier_Destroy(ThreadBarrier* const me); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /chapter2/02-observer/HistogramDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef HistogramDisplay_H 2 | #define HistogramDisplay_H 3 | 4 | #include "ECGPkg.h" 5 | 6 | struct TMDQueue; 7 | 8 | /* class HistogramDisplay */ 9 | typedef struct HistogramDisplay HistogramDisplay; 10 | struct HistogramDisplay { 11 | int index; 12 | struct TMDQueue* itsTMDQueue; 13 | }; 14 | 15 | /* Constructors and destructors:*/ 16 | void HistogramDisplay_Init(HistogramDisplay* const me); 17 | void HistogramDisplay_Cleanup(HistogramDisplay* const me); 18 | 19 | /* Operations */ 20 | void HistogramDisplay_getValue(HistogramDisplay* const me); 21 | void HistogramDisplay_updateHistogram(HistogramDisplay* const me); 22 | struct TMDQueue* HistogramDisplay_getItsTMDQueue(const HistogramDisplay* const me); 23 | void HistogramDisplay_setItsTMDQueue(HistogramDisplay* const me, struct TMDQueue* p_TMDQueue); 24 | 25 | HistogramDisplay * HistogramDisplay_Create(void); 26 | void HistogramDisplay_Destroy(HistogramDisplay* const me); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /chapter2/01-client-server/HistogramDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef HistogramDisplay_H 2 | #define HistogramDisplay_H 3 | 4 | #include "ECGPkg.h" 5 | 6 | struct TMDQueue; 7 | 8 | /* class HistogramDisplay */ 9 | typedef struct HistogramDisplay HistogramDisplay; 10 | struct HistogramDisplay { 11 | int index; 12 | struct TMDQueue* itsTMDQueue; 13 | }; 14 | 15 | /* Constructors and destructors:*/ 16 | void HistogramDisplay_Init(HistogramDisplay* const me); 17 | void HistogramDisplay_Cleanup(HistogramDisplay* const me); 18 | 19 | /* Operations */ 20 | void HistogramDisplay_getValue(HistogramDisplay* const me); 21 | void HistogramDisplay_updateHistogram(HistogramDisplay* const me); 22 | struct TMDQueue* HistogramDisplay_getItsTMDQueue(const HistogramDisplay* const me); 23 | void HistogramDisplay_setItsTMDQueue(HistogramDisplay* const me, struct TMDQueue* p_TMDQueue); 24 | 25 | HistogramDisplay * HistogramDisplay_Create(void); 26 | void HistogramDisplay_Destroy(HistogramDisplay* const me); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /chapter1/01-Sensor/Sensor.h: -------------------------------------------------------------------------------- 1 | #ifndef Sensor_H 2 | #define Sensor_H 3 | 4 | #define WRITE_ADDR 0x1111 5 | #define WRITE_MASK 0x2222 6 | #define READ_ADDR 0x3333 7 | #define SENSOR_PORT 0x4444 8 | 9 | typedef enum _interfaceType 10 | { 11 | MEMORY_MAPPED, 12 | PORT_MAPPED, 13 | MAPPED_MAX 14 | } interfaceType; 15 | 16 | // class sensor 17 | typedef struct Sensor Sensor; 18 | struct Sensor 19 | { 20 | int filterFrequency; 21 | int updateFrequency; 22 | int value; 23 | int whatKindOfInterface; 24 | }; 25 | 26 | int Sensor_getFilterFrequency(const Sensor* const me); 27 | void Sensor_setFilterFrequency(Sensor* const me, int p_filterFrequency); 28 | 29 | int Sensor_getUpdateFrequency(const Sensor* const me); 30 | void Sensor_setUpdateFrequency(Sensor* const me, int p_updateFrequency); 31 | 32 | int Sensor_getValue(const Sensor* const me); 33 | int Sensor_acquireValue(Sensor* me); 34 | 35 | Sensor* Sensor_Create(void); 36 | void Sensor_Destroy(Sensor* const me); 37 | 38 | #endif //Sensor_H 39 | -------------------------------------------------------------------------------- /chapter4/07-simulaLock/GPSPositionSensor.c: -------------------------------------------------------------------------------- 1 | #include "GPSPositionSensor.h" 2 | 3 | void GPSPositionSensor_Init(GPSPositionSensor* const me){ 4 | 5 | } 6 | void GPSPositionSensor_Cleanup(GPSPositionSensor* const me){ 7 | 8 | } 9 | 10 | GPSPositionSensor* GPSPositionSensor_Create(void){ 11 | GPSPositionSensor* me = (GPSPositionSensor*)malloc(sizeof(GPSPositionSensor)); 12 | if (me != NULL) 13 | { 14 | GPSPositionSensor_Init(me); 15 | } 16 | return me; 17 | } 18 | void GPSPositionSensor_Destroy(GPSPositionSensor* const me){ 19 | if (me != NULL) 20 | { 21 | GPSPositionSensor_Cleanup(me); 22 | } 23 | free(me); 24 | } 25 | 26 | void GPSPositionSensor_configure(GPSPositionSensor* const me, int reqSatellites, int useFast){ 27 | 28 | } 29 | 30 | void GPSPositionSensor_activate(GPSPositionSensor* const me){ 31 | 32 | } 33 | void GPSPositionSensor_deactivate(GPSPositionSensor* const me){ 34 | 35 | } 36 | 37 | int GPSPositionSensor_getPosition(GPSPositionSensor* const me){ 38 | 39 | } 40 | -------------------------------------------------------------------------------- /chapter4/04-guard/KinematicData.h: -------------------------------------------------------------------------------- 1 | #ifndef KinematicData_H 2 | #define KinematicData_H 3 | 4 | #include "GuardedCallExample.h" 5 | #include "Attitude.h" 6 | #include "OSSemaphore.h" 7 | #include "Position.h" 8 | 9 | typedef struct KinematicData KinematicData; 10 | struct KinematicData { 11 | struct Attitude attitude; 12 | struct Position position; 13 | OSSemaphore* sema; /*## mutex semaphore */ 14 | }; 15 | 16 | /* Constructors and destructors:*/ 17 | void KinematicData_Init(KinematicData* const me); 18 | void KinematicData_Cleanup(KinematicData* const me); 19 | 20 | /* Operations */ 21 | 22 | Attitude KinematicData_getAttitude(KinematicData* const me); 23 | struct Position KinematicData_getPosition(KinematicData* const me); 24 | 25 | void KinematicData_setAttitude(KinematicData* const me, Attitude a); 26 | 27 | void KinematicData_setPosition(KinematicData* const me, Position p); 28 | 29 | KinematicData * KinematicData_Create(void); 30 | 31 | void KinematicData_Destroy(KinematicData* const me); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /chapter4/07-simulaLock/DopplerSpeedSensor.c: -------------------------------------------------------------------------------- 1 | #include "DopplerSpeedSensor.h" 2 | 3 | void DopplerSpeedSensor_Init(DopplerSpeedSensor* const me){ 4 | 5 | } 6 | void DopplerSpeedSensor_Cleanup(DopplerSpeedSensor* const me){ 7 | 8 | } 9 | DopplerSpeedSensor* DopplerSpeedSensor_Create(void){ 10 | DopplerSpeedSensor* me = (DopplerSpeedSensor*)malloc(sizeof(DopplerSpeedSensor)); 11 | if (me != NULL) 12 | { 13 | DopplerSpeedSensor_Init(me); 14 | } 15 | return me; 16 | } 17 | 18 | void DopplerSpeedSensor_Destroy(DopplerSpeedSensor* const me){ 19 | if (me != NULL) 20 | { 21 | DopplerSpeedSensor_Cleanup(me); 22 | } 23 | free(me); 24 | } 25 | 26 | void DopplerSpeedSensor_configure(DopplerSpeedSensor* const me, int sampleRate){ 27 | 28 | } 29 | 30 | int DopplerSpeedSensor_getSpeed(DopplerSpeedSensor* const me){ 31 | 32 | } 33 | 34 | void DopplerSpeedSensor_enable(DopplerSpeedSensor* const me){ 35 | 36 | } 37 | 38 | void DopplerSpeedSensor_disable(DopplerSpeedSensor* const me){ 39 | 40 | } 41 | -------------------------------------------------------------------------------- /chapter5/01-singleReceptor/TokenizeAsyncSignlePkg.h: -------------------------------------------------------------------------------- 1 | #ifndef TokenizeAsyncSingle_H 2 | #define TokenizeAsyncSingle_H 3 | 4 | /* this is the size of the event queue */ 5 | #define QSIZE 100 6 | 7 | typedef enum EventType { 8 | EVDIGIT, 9 | EVDOT, 10 | EVWHITESPACE, 11 | EVENDOFSTRING 12 | } EventType; 13 | 14 | typedef struct { 15 | EventType eType; 16 | union eventData { 17 | char c; 18 | } ed; 19 | } Event; 20 | 21 | typedef enum TSTATETYPE { 22 | NULL_STATE, 23 | NONUMBER_STATE, 24 | GOTNUMBER_STATE 25 | } TSTATETYPE; 26 | 27 | typedef enum TSUBSTATETYPE { 28 | NULL_SSTATE, 29 | PROCESSINGWHOLEPART_SSTATE, 30 | PROCESSINGFRACTIONALPART_SSTATE 31 | } TSUBSTATETYPE; 32 | 33 | 34 | /* helper function returns the digit */ 35 | /* held by a char */ 36 | int digit(char c) { 37 | return c-'0'; 38 | } 39 | 40 | /* 41 | OS signal and wait functions for 42 | task synchronization 43 | */ 44 | void postSignal(void); 45 | void waitOnSignal(void); 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /chapter6/05-protectsinglechannel/Thermostat.h: -------------------------------------------------------------------------------- 1 | #ifndef Thermostat_H 2 | #define Thermostat_H 3 | 4 | struct FurnaceController; 5 | 6 | typedef struct Thermostat Thermostat; 7 | struct Thermostat { 8 | int defaultTempSetting; 9 | int desiredTemp; 10 | int invertedDefaultTemp; 11 | int invertedDesiredTemp; 12 | struct FurnaceController* itsFurnaceController; 13 | }; 14 | 15 | /* Constructors and destructors:*/ 16 | void Thermostat_Init(Thermostat* const me); 17 | void Thermostat_Cleanup(Thermostat* const me); 18 | 19 | /* Operations */ 20 | int Thermostat_getDesiredTemperature(Thermostat* const me); 21 | 22 | void Thermostat_setDesiredTemperature(Thermostat* const me, int temp); 23 | 24 | struct FurnaceController* Thermostat_getItsFurnaceController(const Thermostat* const me); 25 | 26 | void Thermostat_setItsFurnaceController(Thermostat* const me, struct FurnaceController* p_FurnaceController); 27 | 28 | Thermostat * Thermostat_Create(void); 29 | 30 | void Thermostat_Destroy(Thermostat* const me); 31 | #endif 32 | -------------------------------------------------------------------------------- /chapter3/01-hardwareproxy/bad/bitmapping.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define TURN_OFF (0x00) 5 | #define INITIALIZE (0x61) 6 | #define RUN (0x69) 7 | #define CHECK_ERROR (0x02) 8 | #define DEVICE_ADDRESS (0x01FFAFD0) 9 | 10 | void emergencyShutDown(void){ 11 | printf("OMG We're all gonna die!\n"); 12 | } 13 | 14 | int main() { 15 | unsigned char* pDevice; 16 | 17 | pDevice = (unsigned char *)DEVICE_ADDRESS; // pt to device 18 | // for testing you can replace the above line with 19 | // pDevice = malloc(1); 20 | *pDevice = 0xFF; // start with all bits on 21 | printf ("Device bits %X\n", *pDevice); 22 | *pDevice = *pDevice & INITIALIZE; // and the bits into 23 | printf ("Device bits %X\n", *pDevice); 24 | if (*pDevice & CHECK_ERROR) { // system fail bit on? 25 | emergencyShutDown(); 26 | abort(); 27 | } 28 | else 29 | { 30 | *pDevice = *pDevice & RUN; 31 | printf ("Device bits %X\n", *pDevice); 32 | }; 33 | return 0; 34 | }; 35 | -------------------------------------------------------------------------------- /chapter3/07-poll/BCTimer.c: -------------------------------------------------------------------------------- 1 | #include "BCTimer.h" 2 | 3 | void BCTimer_Init(BCTimer* const me){ 4 | 5 | } 6 | void BCTimer_Cleanup(BCTimer* const me){ 7 | 8 | } 9 | 10 | BCTimer* BCTimer_Create(){ 11 | BCTimer* me = (BCTimer*)malloc(sizeof(BCTimer)); 12 | if (me != NULL) 13 | { 14 | BCTimer_Init(me); 15 | } 16 | return me; 17 | } 18 | 19 | void BCTimer_Destroy(BCTimer* me){ 20 | if (me != NULL) 21 | { 22 | BCTimer_Cleanup(me); 23 | } 24 | free(me); 25 | } 26 | 27 | void BCTimer_startTimer(BCTimer* const me, int pollTime){ 28 | 29 | } 30 | 31 | void BCTimer_stopTimer(BCTimer* const me){ 32 | 33 | } 34 | 35 | void BCTimer_installInterruptHandler(BCTimer* const me){ 36 | 37 | } 38 | 39 | void BCTimer_removeInterruptHandler(BCTimer* const me){ 40 | 41 | } 42 | 43 | void BCTimer_setItsBCPeriodicPoller(BCTimer* const me, BCPeriodicPoller* const poller){ 44 | me->poller = poller; 45 | } 46 | 47 | BCPeriodicPoller* BCTimer_getItsBCPeriodicPoller(BCTimer* const me){ 48 | return me->poller; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /chapter4/04-guard/AttitudeController.h: -------------------------------------------------------------------------------- 1 | #ifndef AttitudeController_H 2 | #define AttitudeController_H 3 | 4 | #include "GuardedCallExample.h" 5 | #include "Attitude.h" 6 | struct KinematicData; 7 | 8 | typedef struct AttitudeController AttitudeController; 9 | struct AttitudeController { 10 | struct Attitude ownShipAttitude; 11 | struct KinematicData* itsKinematicData; 12 | }; 13 | 14 | 15 | /* Constructors and destructors:*/ 16 | 17 | void AttitudeController_Init(AttitudeController* const me); 18 | void AttitudeController_Cleanup(AttitudeController* const me); 19 | 20 | 21 | /* Operations */ 22 | 23 | void AttitudeController_manageAttitude(AttitudeController* const me); 24 | 25 | struct KinematicData* AttitudeController_getItsKinematicData(const AttitudeController* const me); 26 | 27 | void AttitudeController_setItsKinematicData(AttitudeController* const me, struct KinematicData* p_KinematicData); 28 | 29 | AttitudeController * AttitudeController_Create(void); 30 | 31 | void AttitudeController_Destroy(AttitudeController* const me); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /chapter4/04-guard/Attitude.c: -------------------------------------------------------------------------------- 1 | #include "Attitude.h" 2 | 3 | void Attitude_Init(Attitude* const me) { 4 | } 5 | 6 | void Attitude_Cleanup(Attitude* const me) { 7 | } 8 | 9 | double Attitude_getPitch(Attitude* const me) { 10 | return me->pitch; 11 | } 12 | 13 | double Attitude_getRoll(Attitude* const me) { 14 | return me->roll; 15 | } 16 | 17 | double Attitude_getYaw(Attitude* const me) { 18 | return me->yaw; 19 | } 20 | 21 | void Attitude_setPitch(Attitude* const me, double x) { 22 | me->pitch = x; 23 | } 24 | 25 | void Attitude_setRoll(Attitude* const me, double x) { 26 | me->roll = x; 27 | } 28 | 29 | void Attitude_setYaw(Attitude* const me, double x) { 30 | me->yaw = x; 31 | } 32 | 33 | Attitude * Attitude_Create(void) { 34 | Attitude* me = (Attitude *) malloc(sizeof(Attitude)); 35 | if(me!=NULL) 36 | { 37 | Attitude_Init(me); 38 | } 39 | return me; 40 | } 41 | 42 | void Attitude_Destroy(Attitude* const me) { 43 | if(me!=NULL) 44 | { 45 | Attitude_Cleanup(me); 46 | } 47 | free(me); 48 | } 49 | -------------------------------------------------------------------------------- /chapter4/04-guard/Position.c: -------------------------------------------------------------------------------- 1 | #include "Position.h" 2 | 3 | void Position_Init(Position* const me) { 4 | } 5 | 6 | void Position_Cleanup(Position* const me) { 7 | } 8 | 9 | double Position_getAltitude(Position* const me) { 10 | return me->altitude; 11 | } 12 | 13 | double Position_getLatitude(Position* const me) { 14 | return me->latitude; 15 | } 16 | 17 | double Position_getLongitude(Position* const me) { 18 | return me->longitude; 19 | } 20 | 21 | void Position_setAltitude(Position* const me, double x) { 22 | me->altitude = x; 23 | } 24 | 25 | void Position_setLatitude(Position* const me, double x) { 26 | me->latitude = x; 27 | } 28 | 29 | void Position_setLongitude(Position* const me, double x) { 30 | me->longitude = x; 31 | } 32 | 33 | Position * Position_Create(void) { 34 | Position* me = (Position *) malloc(sizeof(Position)); 35 | if(me!=NULL) { 36 | Position_Init(me); 37 | } 38 | return me; 39 | } 40 | 41 | void Position_Destroy(Position* const me) { 42 | if(me!=NULL) { 43 | Position_Cleanup(me); 44 | } 45 | free(me); 46 | } 47 | -------------------------------------------------------------------------------- /chapter4/02-staticpriority/MotorDisplay.c: -------------------------------------------------------------------------------- 1 | #include "MotorData.h" 2 | #include 3 | #include "MotorDisplay.h" 4 | 5 | static int md_priority = 10; 6 | static int md_eventQueue[10]; 7 | static int md_stack[1024]; 8 | 9 | void MotorDisplay_Init(void) { 10 | /* create the OS event queue and the task */ 11 | /* use a queue of 10 events, and a 1024 element stack */ 12 | OSQCreate (&md_eventQueue[0], 10); 13 | OSTaskCreate (MotorDisplay_run, NULL, (void *)&md_stack[1024], 5); 14 | 15 | /* now run the task */ 16 | MotorDisplay_run(); 17 | } 18 | 19 | void MotorDisplay_Cleanup(void) { 20 | } 21 | 22 | void MotorDisplay_run(void) { 23 | int os_pend_timer_id; 24 | os_pend_timer_id = OSCreateTimer(500, RETRIGGER); 25 | 26 | while (TRUE) { 27 | OSQPend(os_pend_timer_id, WAIT_FOREVER); 28 | display(); 29 | }; 30 | } 31 | 32 | void display(void) { 33 | printf("Commanded position = %d\n", getCmdPos()); 34 | printf("Measured position = %d\n\n", getMeasPos()); 35 | } 36 | 37 | void MotorDisplay_initRelations(void) { 38 | MotorDisplay_Init(); 39 | } 40 | -------------------------------------------------------------------------------- /chapter2/01-client-server/ArrhythmiaDetector.h: -------------------------------------------------------------------------------- 1 | #ifndef ArrhythmiaDetector_H 2 | #define ArrhythmiaDetector_H 3 | 4 | #include "ECGPkg.h" 5 | 6 | struct TMDQueue; 7 | 8 | typedef struct ArrhythmiaDetector ArrhythmiaDetector; 9 | struct ArrhythmiaDetector 10 | { 11 | int pcvCount; 12 | int STSegmentHeight; 13 | int firstDegreeHeatBlock; 14 | int Two_one_heartBlock; 15 | int prematureAtrialContraction; 16 | int flbrillation; 17 | int index; 18 | struct TMDQueue* itsTMDQueue; 19 | }; 20 | 21 | // constructions and desctructions 22 | void ArrhythmiaDetector_Init(ArrhythmiaDetector* const me); 23 | void ArrhythmiaDetector_Cleanup(ArrhythmiaDetector* const me); 24 | 25 | void ArrhythmiaDetector_indentifyArrhythmia(ArrhythmiaDetector* const me); 26 | void ArrhythmiaDetector_getDataSample(ArrhythmiaDetector* const me); 27 | void ArrhythmiaDetector_setItsTMDQueue(ArrhythmiaDetector* const me, struct TMDQueue* p_TMDQueue); 28 | 29 | ArrhythmiaDetector * ArrhythmiaDetector_Create(void); 30 | ArrhythmiaDetector * ArrhythmiaDetector_Destroy(ArrhythmiaDetector* const me); 31 | 32 | #endif //ArrhythmiaDetector_H 33 | -------------------------------------------------------------------------------- /chapter5/04-StatePattern/Context.h: -------------------------------------------------------------------------------- 1 | #ifndef Context_H 2 | #define Context_H 3 | 4 | #include "StatePattern.h" 5 | #include "GN_ProcessingFractionState.h" 6 | #include "GN_ProcessingWholeState.h" 7 | #include "NoNumberState.h" 8 | 9 | struct Mutex; 10 | 11 | 12 | typedef struct Context Context; 13 | struct Context { 14 | char ch; 15 | TSTATETYPE currentState; 16 | double result; 17 | struct NoNumberState* stateList[GN_PROCESSINGFRACTIONALPART_STATE+1]; 18 | double tensPlace; 19 | struct Mutex* itsMutex; 20 | }; 21 | 22 | /* Constructors and destructors:*/ 23 | void Context_Init(Context* const me); 24 | void Context_Cleanup(Context* const me); 25 | 26 | /* Operations */ 27 | void Context_evDigit(Context* const me, char c); 28 | void Context_evDot(Context* const me); 29 | void Context_evEndOfString(Context* const me); 30 | void Context_evWhiteSpace(Context* const me); 31 | 32 | 33 | struct Mutex* Context_getItsMutex(const Context* const me); 34 | void Context_setItsMutex(Context* const me, struct Mutex* p_Mutex); 35 | 36 | Context * Context_Create(void); 37 | void Context_Destroy(Context* const me); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /chapter4/08-orderLock/ResourceList.h: -------------------------------------------------------------------------------- 1 | #ifndef ResourceList_H 2 | #define ResourceList_H 3 | 4 | #include "OrderedLockingExample.h" 5 | struct Mutex; 6 | 7 | typedef struct ResourceList ResourceList; 8 | struct ResourceList { 9 | int nIDs; 10 | int rIDList[20]; 11 | struct Mutex* itsMutex; 12 | }; 13 | 14 | /* Constructors and destructors:*/ 15 | void ResourceList_Init(ResourceList* const me); 16 | void ResourceList_Cleanup(ResourceList* const me); 17 | 18 | /* Operations */ 19 | int ResourceList_addLock(ResourceList* const me, int rID); 20 | 21 | /* The elements are added in order but */ 22 | /* can be removed in any order. Therefore, */ 23 | /* the stored resoiurceIDs above the current */ 24 | /* one in the list must be moved down to */ 25 | /* lower in the list. */ 26 | void ResourceList_removeLock(ResourceList* const me, int rID); 27 | 28 | struct Mutex* ResourceList_getItsMutex(const ResourceList* const me); 29 | 30 | void ResourceList_setItsMutex(ResourceList* const me, struct Mutex* p_Mutex); 31 | 32 | ResourceList * ResourceList_Create(void); 33 | 34 | void ResourceList_Destroy(ResourceList* const me); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /chapter2/01-client-server/TMDQueue.h: -------------------------------------------------------------------------------- 1 | #ifndef TMDQueue_H 2 | #define TMDQueue_H 3 | 4 | /*## auto_generated */ 5 | #include "ECGPkg.h" 6 | #include "TimeMarkedData.h" 7 | 8 | typedef struct TMDQueue TMDQueue; 9 | /* 10 | This queue is meant to operate as a "leaky" queue. In this queue, 11 | data is never removed per se, but is instead overwritten when the 12 | buffer pointer wraps around. This allows for many clients to read the same data from the queue. */ 13 | 14 | struct TMDQueue { 15 | int head; 16 | int size; 17 | struct TimeMarkedData buffer[QUEUE_SIZE]; 18 | }; 19 | 20 | /* Constructors and destructors:*/ 21 | void TMDQueue_Init(TMDQueue* const me); 22 | void TMDQueue_Cleanup(TMDQueue* const me); 23 | 24 | /* Operations */ 25 | int TMDQueue_getNextIndex(TMDQueue* const me, int index); 26 | void TMDQueue_insert(TMDQueue* const me, const struct TimeMarkedData tmd); 27 | boolean TMDQueue_isEmpty(TMDQueue* const me); 28 | struct TimeMarkedData TMDQueue_remove(TMDQueue* const me, int index); 29 | int TMDQueue_getBuffer(const TMDQueue* const me); 30 | 31 | TMDQueue * TMDQueue_Create(void); 32 | void TMDQueue_Destroy(TMDQueue* const me); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /chapter4/02-staticpriority/MotorPositionSensor.c: -------------------------------------------------------------------------------- 1 | #include "MotorData.h" 2 | #include "MotorPositionSensor.h" 3 | #include 4 | 5 | static int mps_priority = 5; 6 | static int mps_eventQueue[10]; 7 | static int mps_stack[1024]; 8 | 9 | void MotorPositionSensor_Cleanup(void) { 10 | } 11 | void MotorPositionSensor_Init(void) { 12 | /* create the OS event queue and the task */ 13 | /* use a queue of 10 events, and a 1024 element stack */ 14 | OSQCreate(&mps_eventQueue[0], 10); 15 | OSTaskCreate(MotorPositionSensor_run, NULL, (void *)&mps_stack[1024], 5); 16 | 17 | /* now run the task */ 18 | MotorPositionSensor_run(); 19 | } 20 | 21 | void MotorPositionSensor_run(void) { 22 | int os_pend_timer_id; 23 | os_pend_timer_id = OSCreateTimer(50, RETRIGGER); 24 | 25 | while (TRUE) { 26 | OSQPend(os_pend_timer_id, WAIT_FOREVER); 27 | getPosition(); 28 | }; 29 | } 30 | 31 | void getPosition(void) { 32 | int x; 33 | /* get position by reading the sensor */ 34 | x = rand(); 35 | setMeasPos(x); 36 | } 37 | 38 | void MotorPositionSensor_initRelations(void) { 39 | MotorPositionSensor_Init(); 40 | } 41 | -------------------------------------------------------------------------------- /chapter4/05-queue/GasDisplay.c: -------------------------------------------------------------------------------- 1 | #include "GasDisplay.h" 2 | #include 3 | 4 | void GasDisplay_Init(GasDisplay* const me) { 5 | me->screenWidth = 80; 6 | } 7 | 8 | void GasDisplay_Cleanup(GasDisplay* const me) { 9 | } 10 | 11 | void GasDisplay_printGasData(GasDisplay* const me, const GAS_TYPE gasType, double gas_conc, unsigned int gas_flow) { 12 | printf("\n"); 13 | switch (gasType) { 14 | case O2_GAS: 15 | printf("Oxygen "); 16 | break; 17 | case N2_GAS: 18 | printf("Nitrogen "); 19 | break; 20 | case HE_GAS: 21 | printf("Helium "); 22 | break; 23 | default: 24 | printf("UNKNWON "); 25 | break; 26 | }; 27 | printf("Conc %f, Flow in CC/Min %d\n", gas_conc, gas_flow); 28 | printf("\n"); 29 | } 30 | 31 | GasDisplay * GasDisplay_Create(void) { 32 | GasDisplay* me = (GasDisplay *) malloc(sizeof(GasDisplay)); 33 | if(me!=NULL) { 34 | GasDisplay_Init(me); 35 | } 36 | return me; 37 | } 38 | 39 | void GasDisplay_Destroy(GasDisplay* const me) { 40 | if(me!=NULL) { 41 | GasDisplay_Cleanup(me); 42 | } 43 | free(me); 44 | } 45 | -------------------------------------------------------------------------------- /chapter6/01-OwnShipAttitude/OwnShipAttitude.h: -------------------------------------------------------------------------------- 1 | #ifndef OwnShipAttitude_H 2 | #define OwnShipAttitude_H 3 | 4 | #include "AttitudeDataType.h" 5 | struct AlarmManager; 6 | 7 | typedef struct OwnShipAttitude OwnShipAttitude; 8 | struct OwnShipAttitude { 9 | struct AttitudeDataType attitude; 10 | struct AttitudeDataType invertedAttitude; 11 | struct AlarmManager* itsAlarmManager; 12 | }; 13 | 14 | 15 | void OwnShipAttitude_Init(OwnShipAttitude* const me); 16 | void OwnShipAttitude_Cleanup(OwnShipAttitude* const me); 17 | 18 | /* Operations */ 19 | void OwnShipAttitude_errorHandler(OwnShipAttitude* const me); 20 | 21 | int OwnShipAttitude_getAttitude(OwnShipAttitude* const me, AttitudeDataType * aPtr); 22 | void OwnShipAttitude_setAttitude(OwnShipAttitude* const me, AttitudeDataType a); 23 | 24 | AttitudeDataType OwnShipAttitude_invert(OwnShipAttitude* const me, AttitudeDataType a); 25 | 26 | struct AlarmManager* OwnShipAttitude_getItsAlarmManager(const OwnShipAttitude* const me); 27 | void OwnShipAttitude_setItsAlarmManager(OwnShipAttitude* const me, struct AlarmManager* p_AlarmManager); 28 | 29 | OwnShipAttitude * OwnShipAttitude_Create(void); 30 | void OwnShipAttitude_Destroy(OwnShipAttitude* const me); 31 | #endif 32 | -------------------------------------------------------------------------------- /chapter3/05-debounce/main.c: -------------------------------------------------------------------------------- 1 | #include "ButtonDriver.h" 2 | #include "Button.h" 3 | #include "MicrowaveEmitter.h" 4 | #include "Timer.h" 5 | #include 6 | #include 7 | 8 | int main(int argc, char const *argv[]) 9 | { 10 | ButtonDriver* p_ButtonDriver = ButtonDriver_Create(); 11 | Button* p_Button = Button_Create(); 12 | MicrowaveEmitter* p_MicrowaveEmitter = MicrowaveEmitter_Create(); 13 | Timer* p_Timer = Timer_Create(); 14 | 15 | p_ButtonDriver->toggleOn = 0; 16 | p_ButtonDriver->oldState = 1; 17 | p_Button->deviceState = 0; 18 | 19 | ButtonDriver_setItsButton(p_ButtonDriver, p_Button); 20 | ButtonDriver_setItsMicrowaveEmitter(p_ButtonDriver, p_MicrowaveEmitter); 21 | ButtonDriver_setItsTimer(p_ButtonDriver, p_Timer); 22 | ButtonDriver_eventReceive(p_ButtonDriver); 23 | 24 | p_ButtonDriver->toggleOn = 1; 25 | p_ButtonDriver->oldState = 1; 26 | p_Button->deviceState = 0; 27 | 28 | ButtonDriver_setItsButton(p_ButtonDriver, p_Button); 29 | ButtonDriver_setItsMicrowaveEmitter(p_ButtonDriver, p_MicrowaveEmitter); 30 | ButtonDriver_setItsTimer(p_ButtonDriver, p_Timer); 31 | ButtonDriver_eventReceive(p_ButtonDriver); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /chapter1/02-Queue/Queue.h: -------------------------------------------------------------------------------- 1 | #ifndef QUEUE_H_ 2 | #define QUEUE_H_ 3 | 4 | #define QUEUE_SIZE 10 5 | 6 | typedef struct Queue Queue; 7 | struct Queue 8 | { 9 | int buffer[QUEUE_SIZE]; // where the data things are 10 | int head; 11 | int size; 12 | int tail; 13 | int (*isFull)(Queue* const me); 14 | int (*isEmpty)(Queue* const me); 15 | int (*getSize)(Queue* const me); 16 | int (*insert)(Queue* const me, int k); 17 | int (*remove)(Queue* const me); 18 | }; 19 | 20 | // constructions and destructors 21 | void Queue_Init(Queue* const me, 22 | int (*isFullFunc)(Queue* const me), 23 | int (*isEmptyFunc)(Queue* const me), 24 | int (*getSizeFunc)(Queue* const me), 25 | int (*insertFunc)(Queue* const me, int k), 26 | int (*removeFunc)(Queue* const me)); 27 | 28 | void Queue_Cleanup(Queue* const me); 29 | 30 | // operations 31 | int Queue_isFull(Queue* const me); 32 | int Queue_isEmpty(Queue* const me); 33 | int Queue_getSize(Queue* const me); 34 | int Queue_insert(Queue* const me, int k); 35 | int Queue_remove(Queue* const me); 36 | 37 | Queue* Queue_Create(void); 38 | void Queue_Destroy(Queue* const me); 39 | 40 | #endif //QUEUE_H_ 41 | -------------------------------------------------------------------------------- /chapter3/04-observer/DisplayClient.h: -------------------------------------------------------------------------------- 1 | #ifndef DisplayClient_H 2 | #define DisplayClient_H 3 | 4 | #include "GasSensor.h" 5 | #include 6 | #include 7 | 8 | typedef struct DisplayClient DisplayClient; 9 | struct DisplayClient { 10 | struct GasData* itsGasData; 11 | struct GasSensor* itsGasSensor; 12 | }; 13 | 14 | 15 | /* Constructors and destructors:*/ 16 | void DisplayClient_Init(DisplayClient* const me); 17 | void DisplayClient_Cleanup(DisplayClient* const me); 18 | 19 | /* Operations */ 20 | void DisplayClient_accept(DisplayClient* const me, struct GasData* g); 21 | void DisplayClient_alarm(DisplayClient* const me, char* alarmMsg); 22 | void DisplayClient_register(DisplayClient* const me); 23 | void DisplayClient_show(DisplayClient* const me); 24 | struct GasData* DisplayClient_getItsGasData(const DisplayClient* const me); 25 | void DisplayClient_setItsGasData(DisplayClient* const me, struct GasData* p_GasData); 26 | struct GasSensor* DisplayClient_getItsGasSensor(const DisplayClient* const me); 27 | void DisplayClient_setItsGasSensor(DisplayClient* const me, struct GasSensor* p_GasSensor); 28 | DisplayClient * DisplayClient_Create(void); 29 | void DisplayClient_Destroy(DisplayClient* const me); 30 | 31 | #endif 32 | 33 | -------------------------------------------------------------------------------- /chapter3/07-poll/MedicalDisplay.c: -------------------------------------------------------------------------------- 1 | #include "MedicalDisplay.h" 2 | 3 | void MedicalDisplay_Init(MedicalDisplay* const me){ 4 | 5 | } 6 | void MedicalDisplay_Cleanup(MedicalDisplay* const me){ 7 | 8 | } 9 | 10 | MedicalDisplay* MedicalDisplay_Create(){ 11 | MedicalDisplay* me = (MedicalDisplay*)malloc(sizeof(MedicalDisplay)); 12 | if (me != NULL) 13 | { 14 | MedicalDisplay_Init(me); 15 | } 16 | return me; 17 | } 18 | 19 | void MedicalDisplay_Destroy(MedicalDisplay* me){ 20 | if (me != NULL) 21 | { 22 | MedicalDisplay_Cleanup(me); 23 | } 24 | free(me); 25 | } 26 | 27 | void MedicalDisplay_showO2Concentration(MedicalDisplay* const me, int data){ 28 | printf("call MedicalDisplay_showO2Concentration, data:%d\n", data); 29 | } 30 | void MedicalDisplay_showGasFlow(MedicalDisplay* const me, int data){ 31 | printf("call MedicalDisplay_showGasFlow, data:%d\n", data); 32 | } 33 | void MedicalDisplay_showGasFlowStatus(MedicalDisplay* const me, int state){ 34 | printf("call MedicalDisplay_showGasFlowStatus, state:%d\n", state); 35 | } 36 | void MedicalDisplay_showCircuitPressure(MedicalDisplay* const me, int data){ 37 | printf("call MedicalDisplay_showCircuitPressure, data:%d\n", data); 38 | } 39 | -------------------------------------------------------------------------------- /chapter5/04-StatePattern/NoNumberState.h: -------------------------------------------------------------------------------- 1 | #ifndef NoNumberState_H 2 | #define NoNumberState_H 3 | 4 | #include "StatePattern.h" 5 | #include "StateActionSet.h" 6 | 7 | struct Context; 8 | 9 | typedef struct NoNumberState NoNumberState; 10 | struct NoNumberState { 11 | struct StateActionSet aSet; 12 | struct Context* itsContext; 13 | }; 14 | 15 | 16 | /* Constructors and destructors:*/ 17 | void NoNumberState_Init(NoNumberState* const me); 18 | void NoNumberState_Cleanup(NoNumberState* const me); 19 | 20 | /* Operations */ 21 | void NoNumberState_NoNum2GN(NoNumberState* const me, char c); 22 | void NoNumberState_entryAction(NoNumberState* const me); 23 | int NoNumberState_evDigit(NoNumberState* const me, char c); 24 | int NoNumberState_evDot(NoNumberState* const me); 25 | int NoNumberState_evEndOfString(NoNumberState* const me); 26 | int NoNumberState_evWhiteSpace(NoNumberState* const me); 27 | void NoNumberState_exitAction(NoNumberState* const me); 28 | 29 | 30 | struct Context* NoNumberState_getItsContext(const NoNumberState* const me); 31 | void NoNumberState_setItsContext(NoNumberState* const me, struct Context* p_Context); 32 | NoNumberState * NoNumberState_Create(void); 33 | void NoNumberState_Destroy(NoNumberState* const me); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /chapter6/02-crc/CRCExample.h: -------------------------------------------------------------------------------- 1 | #ifndef CRCExample_H 2 | #define CRCExample_H 3 | 4 | struct AlarmManager; 5 | struct BloodO2Sensor; 6 | struct CRCBuilderClass; 7 | struct CRCClient2; 8 | struct DrugDeliverySystem; 9 | struct HositalPatientSystem; 10 | struct NIBP; 11 | struct PatientData; 12 | struct Thermometer; 13 | 14 | typedef enum ErrorCodeType { 15 | NO_ERRORS, 16 | UNKNOWN_ERROR, 17 | CORRUPT_DATA, 18 | WEIGHT_TOO_LOW, 19 | WEIGHT_TOO_HIGH, 20 | AGE_TOO_HIGH, 21 | GENDER_OUT_OF_RANGE, 22 | SYSTOLIC_TOO_LOW, 23 | SYSTOLIC_TOO_HIGH, 24 | DIASTOLIC_TOO_LOW, 25 | DIASTOLIC_TOO_HIGH, 26 | TEMPERATURE_TOO_LOW, 27 | TEMPERATIURE_TOO_HIGH, 28 | BLOOD_O2_TOO_LOW, 29 | BLOOD_O2_TOO_HIGH 30 | } ErrorCodeType; 31 | 32 | typedef enum GenderType { 33 | MALE, 34 | FEMALE, 35 | HERMAPHRODITE 36 | } GenderType; 37 | 38 | typedef struct PatientDataType { 39 | unsigned short age; 40 | unsigned short bloodO2Conc; 41 | unsigned short diastolicBP; 42 | GenderType gender; 43 | unsigned short heartRate; 44 | char name[100]; 45 | unsigned long patientID; 46 | unsigned short systolicBP; 47 | unsigned short temperature; 48 | double weight; 49 | } PatientDataType; 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /chapter3/02-adapter/AcmeO2Adapter.c: -------------------------------------------------------------------------------- 1 | #include "AcmeO2Adapter.h" 2 | 3 | void AcmeO2Adapter_Init(AcmeO2Adapter* const me) { 4 | me->itsAcmeO2SensorProxy = (AcmeO2SensorProxy*)malloc(sizeof(AcmeO2SensorProxy)); 5 | if (me->itsAcmeO2SensorProxy != NULL) 6 | { 7 | me->itsAcmeO2SensorProxy->getO2Conc = getO2Conc; 8 | me->itsAcmeO2SensorProxy->getO2Flow = getO2Flow; 9 | } 10 | } 11 | 12 | void AcmeO2Adapter_Cleanup(AcmeO2Adapter* const me) { 13 | if (me != NULL && me->itsAcmeO2SensorProxy != NULL) 14 | { 15 | free(me->itsAcmeO2SensorProxy); 16 | } 17 | } 18 | 19 | AcmeO2Adapter* AcmeO2Adapter_Create() { 20 | AcmeO2Adapter* me = (AcmeO2Adapter*)malloc(sizeof(AcmeO2Adapter)); 21 | if (me != NULL) 22 | { 23 | AcmeO2Adapter_Init(me); 24 | } 25 | return me; 26 | } 27 | 28 | void AcmeO2Adapter_Destroy(AcmeO2Adapter* me){ 29 | if (me != NULL) 30 | { 31 | AcmeO2Adapter_Destroy(me); 32 | } 33 | free(me); 34 | } 35 | 36 | int AcmeO2Adapter_gimmeO2Conc(AcmeO2Adapter* const me) { 37 | return me->itsAcmeO2SensorProxy->getO2Conc(); 38 | } 39 | 40 | int AcmeO2Adapter_gimmeO2Flow(AcmeO2Adapter* const me) { 41 | return (int)(me->itsAcmeO2SensorProxy->getO2Flow()*60)/100; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /chapter1/03-SecuritySupervisor/SecuritySupervisor.h: -------------------------------------------------------------------------------- 1 | #ifndef SecuritySupervisor_H 2 | #define SecuritySupervisor_H 3 | 4 | #define Null_id 0 5 | #define keypress_SecuritySupervisor_Event_id 1 6 | 7 | #define ESC_KEY 'c' 8 | #define ENTER_KEY 'e' 9 | #define RESET_KEY 'r' 10 | 11 | typedef enum ActiveState 12 | { 13 | SecuritySupervisor_Idle, 14 | SecuritySupervisor_Accepting, 15 | SecuritySupervisor_CheckingLength, 16 | SecuritySupervisor_ValidatingPIN, 17 | SecuritySupervisor_SecurityOpen, 18 | SecuritySupervisor_ErrorState 19 | } ActiveState; 20 | 21 | typedef enum EventStatus 22 | { 23 | EventNotConsumed, 24 | EventConsumed 25 | } EventStatus; 26 | 27 | typedef struct Param Param; 28 | struct Param 29 | { 30 | int key; 31 | }; 32 | 33 | typedef struct DoorMachineState DoorMachineState; 34 | struct DoorMachineState 35 | { 36 | Param* params; 37 | ActiveState activeState; 38 | int retries; 39 | char id; 40 | }; 41 | 42 | 43 | void displayMsg(char* msg); 44 | EventStatus dispatchEvent(DoorMachineState* door); 45 | int isDigit(char key); 46 | int isEnter(char key); 47 | int isValid(char* pin); 48 | int isCancel(char key); 49 | int isReset(char key); 50 | void unlockDoor(); 51 | void lockDoor(); 52 | 53 | #endif //SecuritySupervisor_H 54 | -------------------------------------------------------------------------------- /chapter5/05-AndState/ModeStateClass.h: -------------------------------------------------------------------------------- 1 | #ifndef ModeStateClass_H 2 | #define ModeStateClass_H 3 | 4 | #include "LightPkg.h" 5 | #include "StateActionSet.h" 6 | struct LightController; 7 | 8 | typedef struct ModeStateClass ModeStateClass; 9 | struct ModeStateClass { 10 | struct StateActionSet aSet; 11 | ModeStatesType currentState; 12 | ModeStatesType defaultState; 13 | struct LightController* itsLightController; 14 | }; 15 | 16 | void ModeStateClass_Init(ModeStateClass* const me); 17 | void ModeStateClass_Cleanup(ModeStateClass* const me); 18 | 19 | /* Operations */ 20 | void ModeStateClass_entryAction(ModeStateClass* const me); 21 | int ModeStateClass_evAbort(ModeStateClass* const me); 22 | int ModeStateClass_evRun(ModeStateClass* const me); 23 | int ModeStateClass_evShutDown(ModeStateClass* const me); 24 | void ModeStateClass_exitAction(ModeStateClass* const me); 25 | int ModeStateClass_is_in(ModeStateClass* const me, ModeStatesType s); 26 | 27 | struct LightController* ModeStateClass_getItsLightController(const ModeStateClass* const me); 28 | void ModeStateClass_setItsLightController(ModeStateClass* const me, struct LightController* p_LightController); 29 | 30 | ModeStateClass * ModeStateClass_Create(void); 31 | void ModeStateClass_Destroy(ModeStateClass* const me); 32 | #endif 33 | -------------------------------------------------------------------------------- /chapter2/01-client-server/ArrhythmiaDetector.c: -------------------------------------------------------------------------------- 1 | #include "ArrhythmiaDetector.h" 2 | #include "TMDQueue.h" 3 | 4 | void ArrhythmiaDetector_Init(ArrhythmiaDetector* const me){ 5 | 6 | } 7 | 8 | void ArrhythmiaDetector_Cleanup(ArrhythmiaDetector* const me){ 9 | 10 | } 11 | 12 | void ArrhythmiaDetector_indentifyArrhythmia(ArrhythmiaDetector* const me){ 13 | 14 | } 15 | 16 | void ArrhythmiaDetector_getDataSample(ArrhythmiaDetector* const me){ 17 | TimeMarkedData tmd; 18 | tmd = TMDQueue_remove(me->itsTMDQueue, me->index); 19 | printf("ArrhythmiaDetector index: %d TimeInterval: %d DataValue: %d\n", me->index, tmd.timeInterval, tmd.dataValue); 20 | me->index = TMDQueue_getNextIndex(me->itsTMDQueue, me->index); 21 | } 22 | 23 | void ArrhythmiaDetector_setItsTMDQueue(ArrhythmiaDetector* const me, struct TMDQueue* p_TMDQueue){ 24 | me->itsTMDQueue = p_TMDQueue; 25 | } 26 | 27 | ArrhythmiaDetector * ArrhythmiaDetector_Create(void){ 28 | ArrhythmiaDetector* me = (ArrhythmiaDetector*)malloc(sizeof(ArrhythmiaDetector)); 29 | if (me != NULL) 30 | { 31 | ArrhythmiaDetector_Init(me); 32 | } 33 | return me; 34 | } 35 | 36 | ArrhythmiaDetector * ArrhythmiaDetector_Destroy(ArrhythmiaDetector* const me){ 37 | if (me != NULL) 38 | { 39 | ArrhythmiaDetector_Cleanup(me); 40 | } 41 | free(me); 42 | } 43 | -------------------------------------------------------------------------------- /chapter4/04-guard/Navigator.c: -------------------------------------------------------------------------------- 1 | #include "Navigator.h" 2 | #include "KinematicData.h" 3 | 4 | static void cleanUpRelations(Navigator* const me); 5 | 6 | void Navigator_Init(Navigator* const me) { 7 | Position_Init(&(me->ownShipPosition)); 8 | me->itsKinematicData = NULL; 9 | } 10 | 11 | void Navigator_Cleanup(Navigator* const me) { 12 | cleanUpRelations(me); 13 | } 14 | 15 | void Navigator_updatePosition(Navigator* const me) { 16 | KinematicData_setPosition(me->itsKinematicData, me->ownShipPosition); 17 | } 18 | 19 | struct KinematicData* Navigator_getItsKinematicData(const Navigator* const me) { 20 | return (struct KinematicData*)me->itsKinematicData; 21 | } 22 | 23 | void Navigator_setItsKinematicData(Navigator* const me, struct KinematicData* p_KinematicData) { 24 | me->itsKinematicData = p_KinematicData; 25 | } 26 | 27 | Navigator * Navigator_Create(void) { 28 | Navigator* me = (Navigator *) malloc(sizeof(Navigator)); 29 | if(me!=NULL) { 30 | Navigator_Init(me); 31 | } 32 | return me; 33 | } 34 | 35 | void Navigator_Destroy(Navigator* const me) { 36 | if(me!=NULL) { 37 | Navigator_Cleanup(me); 38 | } 39 | free(me); 40 | } 41 | 42 | static void cleanUpRelations(Navigator* const me) { 43 | if(me->itsKinematicData != NULL) { 44 | me->itsKinematicData = NULL; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /chapter2/02-observer/NotificationHandle.c: -------------------------------------------------------------------------------- 1 | #include "NotificationHandle.h" 2 | 3 | static void cleanUpRelations(NotificationHandle* const me); 4 | 5 | void NotificationHandle_Init(NotificationHandle* const me) { 6 | me->itsNotificationHandle = NULL; 7 | } 8 | 9 | void NotificationHandle_Cleanup(NotificationHandle* const me) { 10 | cleanUpRelations(me); 11 | } 12 | 13 | struct NotificationHandle* NotificationHandle_getItsNotificationHandle(const NotificationHandle* const me) { 14 | return (struct NotificationHandle*)me->itsNotificationHandle; 15 | } 16 | 17 | void NotificationHandle_setItsNotificationHandle(NotificationHandle* const me, struct NotificationHandle* p_NotificationHandle) { 18 | me->itsNotificationHandle = p_NotificationHandle; 19 | } 20 | 21 | NotificationHandle * NotificationHandle_Create(void) { 22 | NotificationHandle* me = (NotificationHandle *) malloc(sizeof(NotificationHandle)); 23 | if(me!=NULL) 24 | { 25 | NotificationHandle_Init(me); 26 | } 27 | return me; 28 | } 29 | 30 | void NotificationHandle_Destroy(NotificationHandle* const me) { 31 | if(me!=NULL) 32 | { 33 | NotificationHandle_Cleanup(me); 34 | } 35 | free(me); 36 | } 37 | 38 | static void cleanUpRelations(NotificationHandle* const me) { 39 | if(me->itsNotificationHandle != NULL) 40 | { 41 | me->itsNotificationHandle = NULL; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /chapter5/05-AndState/ErrorStateClass.h: -------------------------------------------------------------------------------- 1 | #ifndef ErrorStateClass_H 2 | #define ErrorStateClass_H 3 | 4 | #include "LightPkg.h" 5 | #include "StateActionSet.h" 6 | 7 | struct LightController; 8 | 9 | 10 | typedef struct ErrorStateClass ErrorStateClass; 11 | struct ErrorStateClass { 12 | struct StateActionSet aSet; 13 | ErrorStatesType currentState; 14 | ErrorStatesType defaultState; 15 | struct LightController* itsLightController; 16 | }; 17 | 18 | 19 | 20 | void ErrorStateClass_Init(ErrorStateClass* const me); 21 | void ErrorStateClass_Cleanup(ErrorStateClass* const me); 22 | 23 | 24 | /* Operations */ 25 | void ErrorStateClass_entryAction(ErrorStateClass* const me); 26 | int ErrorStateClass_evAbort(ErrorStateClass* const me); 27 | int ErrorStateClass_evError(ErrorStateClass* const me); 28 | int ErrorStateClass_evOk(ErrorStateClass* const me); 29 | int ErrorStateClass_evWarning(ErrorStateClass* const me); 30 | void ErrorStateClass_exitAction(ErrorStateClass* const me); 31 | int ErrorStateClass_is_in(ErrorStateClass* const me, ErrorStatesType s); 32 | 33 | struct LightController* ErrorStateClass_getItsLightController(const ErrorStateClass* const me); 34 | 35 | void ErrorStateClass_setItsLightController(ErrorStateClass* const me, struct LightController* p_LightController); 36 | 37 | ErrorStateClass * ErrorStateClass_Create(void); 38 | 39 | void ErrorStateClass_Destroy(ErrorStateClass* const me); 40 | #endif 41 | -------------------------------------------------------------------------------- /chapter5/05-AndState/LightPkg.h: -------------------------------------------------------------------------------- 1 | #ifndef LightPkg_H 2 | #define LightPkg_H 3 | 4 | struct AndStateList; 5 | struct DecompBuilder; 6 | struct DecompClient; 7 | struct ErrorStateClass; 8 | struct Klaxon; 9 | struct Light; 10 | struct LightController; 11 | struct ModeStateClass; 12 | struct Mutex; 13 | struct StateActionSet; 14 | 15 | typedef enum HighLevelLightStates { 16 | NULL_STATE, 17 | OFF_STATE, 18 | ON_STATE, 19 | EMERGENCY_STATE 20 | } HighLevelLightStates; 21 | 22 | typedef enum ColorType { 23 | RED, 24 | YELLOW, 25 | GREEN 26 | } ColorType; 27 | 28 | typedef enum FlashType { 29 | STEADY, 30 | SLOWLY, 31 | QUICKLY 32 | } FlashType; 33 | 34 | typedef enum ErrorStatesType { 35 | NULL_ERROR_STATE, 36 | OK_STATE, 37 | WARNING_STATE, 38 | ERROR_STATE 39 | } ErrorStatesType; 40 | 41 | typedef enum ModeStatesType { 42 | NULL_MODE_STATE, 43 | OPERATIONAL_STATE, 44 | STARTINGUP_STATE, 45 | SHUTTINGDOWN_STATE 46 | } ModeStatesType; 47 | 48 | typedef enum On_State_Regions { 49 | ERRORSTATUS_ANDSTATE, 50 | MODE_ANDSTATE 51 | } On_State_Regions; 52 | 53 | #define MAX_ANDSTATES 2 54 | 55 | /* This action pointer accepts the me object pointer and no other parameters. */ 56 | typedef int (*ActionPtr0)(void*); 57 | 58 | /* This action pointer accepts the me object pointer and no other parameters. */ 59 | typedef int (*ActionPtr1)(void*, int); 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /chapter4/08-orderLock/VelocitySensor.h: -------------------------------------------------------------------------------- 1 | #ifndef VelocitySensor_H 2 | #define VelocitySensor_H 3 | 4 | /* brings in the error codes */ 5 | #include "OrderedLockingExample.h" 6 | #include "Velocity.h" 7 | struct Mutex; 8 | 9 | struct ResourceList; 10 | 11 | typedef struct VelocitySensor VelocitySensor; 12 | struct VelocitySensor { 13 | int resourceID; 14 | struct Velocity velocity; 15 | struct Mutex* itsMutex; 16 | struct ResourceList* itsResourceList; 17 | }; 18 | 19 | void VelocitySensor_Init(VelocitySensor* const me); 20 | 21 | void VelocitySensor_Cleanup(VelocitySensor* const me); 22 | 23 | /* Operations */ 24 | struct Velocity VelocitySensor_getVelocity(VelocitySensor* const me); 25 | 26 | int VelocitySensor_lockDyadic(VelocitySensor* const me); 27 | 28 | void VelocitySensor_releaseDyadic(VelocitySensor* const me); 29 | 30 | void VelocitySensor_setVelocity(VelocitySensor* const me, struct Velocity v); 31 | 32 | struct Mutex* VelocitySensor_getItsMutex(const VelocitySensor* const me); 33 | 34 | void VelocitySensor_setItsMutex(VelocitySensor* const me, struct Mutex* p_Mutex); 35 | 36 | struct ResourceList* VelocitySensor_getItsResourceList(const VelocitySensor* const me); 37 | 38 | void VelocitySensor_setItsResourceList(VelocitySensor* const me, struct ResourceList* p_ResourceList); 39 | 40 | VelocitySensor * VelocitySensor_Create(void); 41 | 42 | void VelocitySensor_Destroy(VelocitySensor* const me); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /chapter2/01-client-server/TestBuilder.h: -------------------------------------------------------------------------------- 1 | #ifndef TestBuilder_H 2 | #define TestBuilder_H 3 | 4 | #include 5 | #include "ECGPkg.h" 6 | #include "ArrhythmiaDetector.h" 7 | #include "ECG_Module.h" 8 | #include "HistogramDisplay.h" 9 | #include "QRSDetector.h" 10 | #include "TMDQueue.h" 11 | #include "WaveformDisplay.h" 12 | 13 | /* class TestBuilder */ 14 | typedef struct TestBuilder TestBuilder; 15 | struct TestBuilder { 16 | struct ArrhythmiaDetector itsArrhythmiaDetector; 17 | struct ECG_Module itsECG_Module; 18 | struct HistogramDisplay itsHistogramDisplay; 19 | struct QRSDetector itsQRSDetector; 20 | struct TMDQueue itsTMDQueue; 21 | struct WaveformDisplay itsWaveformDisplay; 22 | }; 23 | 24 | /* Constructors and destructors:*/ 25 | void TestBuilder_Init(TestBuilder* const me); 26 | void TestBuilder_Cleanup(TestBuilder* const me); 27 | 28 | struct ArrhythmiaDetector* TestBuilder_getItsArrhythmiaDetector(const TestBuilder* const me); 29 | struct ECG_Module* TestBuilder_getItsECG_Module(const TestBuilder* const me); 30 | struct HistogramDisplay* TestBuilder_getItsHistogramDisplay(const TestBuilder* const me); 31 | struct QRSDetector* TestBuilder_getItsQRSDetector(const TestBuilder* const me); 32 | struct TMDQueue* TestBuilder_getItsTMDQueue(const TestBuilder* const me); 33 | struct WaveformDisplay* TestBuilder_getItsWaveformDisplay(const TestBuilder* const me); 34 | 35 | TestBuilder * TestBuilder_Create(void); 36 | void TestBuilder_Destroy(TestBuilder* const me); 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /chapter6/05-protectsinglechannel/Thermometer.h: -------------------------------------------------------------------------------- 1 | #ifndef Thermometer_H 2 | #define Thermometer_H 3 | 4 | struct CheckTemperature; 5 | struct FurnaceController; 6 | 7 | typedef struct Thermometer Thermometer; 8 | struct Thermometer { 9 | int invertedMeasuredTemp; 10 | int measuredTemp; 11 | struct CheckTemperature* itsCheckTemperature; 12 | struct FurnaceController* itsFurnaceController; 13 | }; 14 | 15 | /* Constructors and destructors:*/ 16 | 17 | void Thermometer_Init(Thermometer* const me); 18 | 19 | void Thermometer_Cleanup(Thermometer* const me); 20 | 21 | /* Operations */ 22 | void Thermometer_acquireMeasuredTemperature(Thermometer* const me); 23 | 24 | int Thermometer_getMeasuredTemperature(Thermometer* const me); 25 | 26 | struct CheckTemperature* Thermometer_getItsCheckTemperature(const Thermometer* const me); 27 | 28 | void Thermometer_setItsCheckTemperature(Thermometer* const me, struct CheckTemperature* p_CheckTemperature); 29 | 30 | struct FurnaceController* Thermometer_getItsFurnaceController(const Thermometer* const me); 31 | 32 | void Thermometer_setItsFurnaceController(Thermometer* const me, struct FurnaceController* p_FurnaceController); 33 | 34 | Thermometer * Thermometer_Create(void); 35 | 36 | void Thermometer_Destroy(Thermometer* const me); 37 | 38 | struct CheckTemperature* Thermometer_getItsCheckTemperature_1(const Thermometer* const me); 39 | 40 | void Thermometer_setItsCheckTemperature_1(Thermometer* const me, struct CheckTemperature* p_CheckTemperature); 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /chapter2/02-observer/TestBuilder.h: -------------------------------------------------------------------------------- 1 | #ifndef TestBuilder_H 2 | #define TestBuilder_H 3 | 4 | #include 5 | #include "ECGPkg.h" 6 | #include "ArrhythmiaDetector.h" 7 | #include "ECG_Module.h" 8 | #include "HistogramDisplay.h" 9 | #include "QRSDetector.h" 10 | #include "TMDQueue.h" 11 | #include "WaveformDisplay.h" 12 | 13 | /* class TestBuilder */ 14 | typedef struct TestBuilder TestBuilder; 15 | struct TestBuilder { 16 | // struct ArrhythmiaDetector itsArrhythmiaDetector; 17 | struct ECG_Module itsECG_Module; 18 | struct HistogramDisplay itsHistogramDisplay; 19 | // struct QRSDetector itsQRSDetector; 20 | struct TMDQueue itsTMDQueue; 21 | // struct WaveformDisplay itsWaveformDisplay; 22 | }; 23 | 24 | /* Constructors and destructors:*/ 25 | void TestBuilder_Init(TestBuilder* const me); 26 | void TestBuilder_Cleanup(TestBuilder* const me); 27 | 28 | // struct ArrhythmiaDetector* TestBuilder_getItsArrhythmiaDetector(const TestBuilder* const me); 29 | struct ECG_Module* TestBuilder_getItsECG_Module(const TestBuilder* const me); 30 | struct HistogramDisplay* TestBuilder_getItsHistogramDisplay(const TestBuilder* const me); 31 | // struct QRSDetector* TestBuilder_getItsQRSDetector(const TestBuilder* const me); 32 | struct TMDQueue* TestBuilder_getItsTMDQueue(const TestBuilder* const me); 33 | // struct WaveformDisplay* TestBuilder_getItsWaveformDisplay(const TestBuilder* const me); 34 | 35 | TestBuilder * TestBuilder_Create(void); 36 | void TestBuilder_Destroy(TestBuilder* const me); 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /chapter3/02-adapter/UltimateO2Adapter.c: -------------------------------------------------------------------------------- 1 | #include "UltimateO2Adapter.h" 2 | 3 | void UltimateO2Adapter_Init(UltimateO2Adapter* const me){ 4 | me->itsUltimateO2SensorProxy = (UltimateO2SensorProxy*)malloc(sizeof(UltimateO2SensorProxy)); 5 | if (me->itsUltimateO2SensorProxy != NULL) 6 | { 7 | me->itsUltimateO2SensorProxy->accessO2Conc = accessO2Conc; 8 | me->itsUltimateO2SensorProxy->accessGasFlow = accessGasFlow; 9 | } 10 | } 11 | 12 | void UltimateO2Adapter_Cleanup(UltimateO2Adapter* const me){ 13 | } 14 | 15 | UltimateO2Adapter* UltimateO2Adapter_Create(){ 16 | UltimateO2Adapter* me = (UltimateO2Adapter*)malloc(sizeof(UltimateO2Adapter)); 17 | if (me != NULL) 18 | { 19 | UltimateO2Adapter_Init(me); 20 | } 21 | return me; 22 | } 23 | 24 | void UltimateO2Adapter_Destroy(UltimateO2Adapter* me){ 25 | if (me != NULL) 26 | { 27 | UltimateO2Adapter_Destroy(me); 28 | } 29 | free(me); 30 | } 31 | 32 | int UltimateO2Adapter_gimmeO2Conc(UltimateO2Adapter* const me) { 33 | return (int)(me->itsUltimateO2SensorProxy->accessO2Conc()*100); 34 | } 35 | 36 | int UltimateO2Adapter_gimmeO2Flow(UltimateO2Adapter* const me) { 37 | double totalFlow; 38 | // convert from liters/hr to cc/min 39 | totalFlow = me->itsUltimateO2SensorProxy->accessGasFlow() * 1000.0/60.0; 40 | // now return the portion of the flow due to oxygen 41 | // and return it as an integer 42 | return (int)(totalFlow * me->itsUltimateO2SensorProxy->accessO2Conc()); 43 | } 44 | -------------------------------------------------------------------------------- /chapter3/03-intermediary/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "RobotArmManager.h" 3 | #include "Action.h" 4 | 5 | int main(int argc, char const *argv[]) 6 | { 7 | int x,y,z,t; 8 | 9 | RobotArmManager* me = RobotArmManager_Create(); 10 | x=1; 11 | y=2; 12 | z=3; 13 | t=4; 14 | RobotArmManager_computeTrajectory(me, x, y, z, t); 15 | x=5; 16 | y=6; 17 | z=7; 18 | t=8; 19 | RobotArmManager_graspAt(me, x, y, z, t); 20 | // struct Action 21 | // { 22 | // int rotatingArmJoint1; 23 | // int rotatingArmJoint2; 24 | // int rotatingArmJoint3; 25 | // int rotatingArmJoint4; 26 | // int slidingArmJoint1; 27 | // int slidingArmJoint2; 28 | // int manipulatorForce; 29 | // int manipulatorOpen; 30 | // }; 31 | Action* action = (Action*)malloc(sizeof(Action)); 32 | action->rotatingArmJoint1 = 11; 33 | action->rotatingArmJoint2 = 11; 34 | action->rotatingArmJoint3 = 11; 35 | action->rotatingArmJoint4 = 11; 36 | action->slidingArmJoint1 = 11; 37 | action->slidingArmJoint2 = 11; 38 | action->manipulatorForce = 11; 39 | action->manipulatorOpen = 11; 40 | RobotArmManager_addItsAction(me, action); 41 | 42 | RotatingArmJoint* p_RotatingArmJoint = (RotatingArmJoint*)malloc(sizeof(RotatingArmJoint)); 43 | RobotArmManager_addItsRotatingArmJoint(me, p_RotatingArmJoint); 44 | 45 | RobotArmManager_executeStep(me); 46 | 47 | RobotArmManager_Destroy(me); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /chapter4/03-critical/CRRobotArmManager.h: -------------------------------------------------------------------------------- 1 | #ifndef CRRobotArmManager_H 2 | #define CRRobotArmManager_H 3 | 4 | #include 5 | #include 6 | 7 | struct CRDisplay; 8 | struct RobotArm; 9 | struct UserInput; 10 | 11 | typedef struct CRRobotArmManager CRRobotArmManager; 12 | struct CRRobotArmManager { 13 | struct CRDisplay* itsCRDisplay; 14 | struct RobotArm* itsRobotArm; 15 | struct UserInput* itsUserInput; 16 | }; 17 | 18 | 19 | /* Constructors and destructors:*/ 20 | 21 | void CRRobotArmManager_Init(CRRobotArmManager* const me); 22 | void CRRobotArmManager_Cleanup(CRRobotArmManager* const me); 23 | 24 | /* Operations */ 25 | void CRRobotArmManager_motorZero(CRRobotArmManager* const me); 26 | void CRRobotArmManager_moveRobotArm(CRRobotArmManager* const me); 27 | struct CRDisplay* CRRobotArmManager_getItsCRDisplay(const CRRobotArmManager* const me); 28 | 29 | void CRRobotArmManager_setItsCRDisplay(CRRobotArmManager* const me, struct CRDisplay* p_CRDisplay); 30 | 31 | struct RobotArm* CRRobotArmManager_getItsRobotArm(const CRRobotArmManager* const me); 32 | 33 | void CRRobotArmManager_setItsRobotArm(CRRobotArmManager* const me, struct RobotArm* p_RobotArm); 34 | 35 | struct UserInput* CRRobotArmManager_getItsUserInput(const CRRobotArmManager* const me); 36 | 37 | void CRRobotArmManager_setItsUserInput(CRRobotArmManager* const me, struct UserInput* p_UserInput); 38 | 39 | CRRobotArmManager * CRRobotArmManager_Create(void); 40 | 41 | void CRRobotArmManager_Destroy(CRRobotArmManager* const me); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /chapter4/05-queue/GasController.c: -------------------------------------------------------------------------------- 1 | #include "GasController.h" 2 | #include "GasDisplay.h" 3 | #include 4 | 5 | static void cleanUpRelations(GasController* const me); 6 | 7 | void GasController_Init(GasController* const me) { 8 | me->itsGasDisplay = NULL; 9 | } 10 | 11 | void GasController_Cleanup(GasController* const me) { 12 | cleanUpRelations(me); 13 | } 14 | 15 | void GasController_handleGasData(GasController* const me, GasData * gPtr) { 16 | if (gPtr) { 17 | GasDisplay_printGasData(me->itsGasDisplay, gPtr->gType, gPtr->conc, gPtr->flowInCCPerMin); 18 | free(gPtr); 19 | }; 20 | 21 | } 22 | 23 | struct GasDisplay* GasController_getItsGasDisplay(const GasController* const me) { 24 | return (struct GasDisplay*)me->itsGasDisplay; 25 | } 26 | 27 | void GasController_setItsGasDisplay(GasController* const me, struct GasDisplay* p_GasDisplay) { 28 | me->itsGasDisplay = p_GasDisplay; 29 | } 30 | 31 | GasController * GasController_Create(void) { 32 | GasController* me = (GasController *) malloc(sizeof(GasController)); 33 | if(me!=NULL) 34 | { 35 | GasController_Init(me); 36 | } 37 | return me; 38 | } 39 | 40 | void GasController_Destroy(GasController* const me) { 41 | if(me!=NULL) 42 | { 43 | GasController_Cleanup(me); 44 | } 45 | free(me); 46 | } 47 | 48 | static void cleanUpRelations(GasController* const me) { 49 | if(me->itsGasDisplay != NULL) 50 | { 51 | me->itsGasDisplay = NULL; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /chapter4/04-guard/AttitudeController.c: -------------------------------------------------------------------------------- 1 | #include "AttitudeController.h" 2 | #include "KinematicData.h" 3 | 4 | static void cleanUpRelations(AttitudeController* const me); 5 | 6 | void AttitudeController_Init(AttitudeController* const me) { 7 | Attitude_Init(&(me->ownShipAttitude)); 8 | me->itsKinematicData = NULL; 9 | } 10 | 11 | void AttitudeController_Cleanup(AttitudeController* const me) { 12 | cleanUpRelations(me); 13 | } 14 | 15 | void AttitudeController_manageAttitude(AttitudeController* const me) { 16 | KinematicData_setAttitude(me->itsKinematicData, me->ownShipAttitude); 17 | } 18 | 19 | struct KinematicData* AttitudeController_getItsKinematicData(const AttitudeController* const me) { 20 | return (struct KinematicData*)me->itsKinematicData; 21 | } 22 | 23 | void AttitudeController_setItsKinematicData(AttitudeController* const me, struct KinematicData* p_KinematicData) { 24 | me->itsKinematicData = p_KinematicData; 25 | } 26 | 27 | AttitudeController * AttitudeController_Create(void) { 28 | AttitudeController* me = (AttitudeController *) malloc(sizeof(AttitudeController)); 29 | if(me!=NULL) { 30 | AttitudeController_Init(me); 31 | } 32 | return me; 33 | } 34 | 35 | void AttitudeController_Destroy(AttitudeController* const me) { 36 | if(me!=NULL) { 37 | AttitudeController_Cleanup(me); 38 | } 39 | free(me); 40 | } 41 | 42 | static void cleanUpRelations(AttitudeController* const me) { 43 | if(me->itsKinematicData != NULL) { 44 | me->itsKinematicData = NULL; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /chapter5/01-singleReceptor/TSREventQueue.h: -------------------------------------------------------------------------------- 1 | #ifndef TSREventQueue_H 2 | #define TSREventQueue_H 3 | 4 | #include "TokenizerAsyncSinglePkg.h" 5 | 6 | struct Mutex; 7 | 8 | typedef struct TSREventQueue TSREventQueue; 9 | struct TSREventQueue { 10 | Event q[100]; 11 | int size; 12 | int head; 13 | int tail; 14 | struct Mutex* itsMutex; 15 | }; 16 | 17 | /* Constructors and destructors:*/ 18 | void TSREventQueue_Init(TSREventQueue* const me); 19 | 20 | void TSREventQueue_Cleanup(TSREventQueue* const me); 21 | 22 | /* Operations */ 23 | 24 | void TSREventQueue_isEmpty(TSREventQueue* const me); 25 | 26 | int TSREventQueue_isFull(TSREventQueue* const me); 27 | 28 | /* puts the passed event in */ 29 | /* the event queue and then */ 30 | /* calls the event receptor of */ 31 | /* the state machine. Note */ 32 | /* if the queue overflows an */ 33 | /* error code (1) is returned. */ 34 | int TSREventQueue_post(TSREventQueue* const me, Event e); 35 | 36 | /* It is assumed that the caller has */ 37 | /* ensured that there is at least one */ 38 | /* event in the queue (via the isEmpty() */ 39 | /* funtion) prior to calling this function. */ 40 | /* Othersize they get a default event. */ 41 | Event TSREventQueue_pull(TSREventQueue* const me); 42 | 43 | struct Mutex* TSREventQueue_getItsMutex(const TSREventQueue* const me); 44 | 45 | void TSREventQueue_setItsMutex(TSREventQueue* const me, struct Mutex* p_Mutex); 46 | 47 | TSREventQueue * TSREventQueue_Create(void); 48 | 49 | void TSREventQueue_Destroy(TSREventQueue* const me); 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /chapter5/04-StatePattern/GN_ProcessingWholeState.h: -------------------------------------------------------------------------------- 1 | #ifndef GN_ProcessingWholeState_H 2 | #define GN_ProcessingWholeState_H 3 | 4 | #include "StatePattern.h" 5 | #include "StateActionSet.h" 6 | 7 | struct Context; 8 | 9 | typedef struct GN_ProcessingWholeState GN_ProcessingWholeState; 10 | struct GN_ProcessingWholeState { 11 | struct StateActionSet aSet; 12 | struct Context* itsContext; 13 | }; 14 | 15 | /* Constructors and destructors:*/ 16 | void GN_ProcessingWholeState_Init(GN_ProcessingWholeState* const me); 17 | void GN_ProcessingWholeState_Cleanup(GN_ProcessingWholeState* const me); 18 | 19 | /* Operations */ 20 | void GN_ProcessingWholeState_entryAction(GN_ProcessingWholeState* const me); 21 | int GN_ProcessingWholeState_evDigit(GN_ProcessingWholeState* const me, char c); 22 | int GN_ProcessingWholeState_evDot(GN_ProcessingWholeState* const me); 23 | int GN_ProcessingWholeState_evEndOfString(GN_ProcessingWholeState* const me); 24 | 25 | int GN_ProcessingWholeState_evWhiteSpace(GN_ProcessingWholeState* const me); 26 | void GN_ProcessingWholeState_exitAction(GN_ProcessingWholeState* const me); 27 | void GN_ProcessingWholeState_printResult(GN_ProcessingWholeState* const me); 28 | 29 | struct Context* GN_ProcessingWholeState_getItsContext(const GN_ProcessingWholeState* const me); 30 | 31 | void GN_ProcessingWholeState_setItsContext(GN_ProcessingWholeState* const me, struct Context* p_Context); 32 | 33 | GN_ProcessingWholeState * GN_ProcessingWholeState_Create(void); 34 | 35 | void GN_ProcessingWholeState_Destroy(GN_ProcessingWholeState* const me); 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /chapter1/03-SecuritySupervisor/SecuritySupervisorTest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SecuritySupervisor.h" 3 | #include 4 | 5 | int main(int argc, char const *argv[]) 6 | { 7 | 8 | EventStatus status = EventNotConsumed; 9 | 10 | DoorMachineState* door = (DoorMachineState*)malloc(sizeof(DoorMachineState)); 11 | Param* params = (Param*)malloc(sizeof(Param)); 12 | 13 | params->key = '1'; 14 | door->params = params; 15 | door->retries = 0; 16 | door->activeState = SecuritySupervisor_Idle; 17 | door->id = keypress_SecuritySupervisor_Event_id; 18 | status = dispatchEvent(door); 19 | 20 | params->key = '2'; 21 | door->params = params; 22 | door->id = keypress_SecuritySupervisor_Event_id; 23 | status = dispatchEvent(door); 24 | 25 | params->key = '3'; 26 | door->params = params; 27 | door->id = keypress_SecuritySupervisor_Event_id; 28 | status = dispatchEvent(door); 29 | 30 | params->key = '4'; 31 | door->params = params; 32 | door->id = keypress_SecuritySupervisor_Event_id; 33 | status = dispatchEvent(door); 34 | 35 | params->key = 'e'; 36 | door->params = params; 37 | door->id = keypress_SecuritySupervisor_Event_id; 38 | status = dispatchEvent(door); 39 | 40 | // params->key = 'c'; 41 | // door->params = params; 42 | // door->id = keypress_SecuritySupervisor_Event_id; 43 | // status = dispatchEvent(door); 44 | 45 | // params->key = 'r'; 46 | // door->params = params; 47 | // door->id = Null_id; 48 | // status = dispatchEvent(door); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /chapter3/05-debounce/ButtonDriver.h: -------------------------------------------------------------------------------- 1 | #ifndef ButtonDriver_H 2 | #define ButtonDriver_H 3 | 4 | 5 | #define LOOPS_PER_MS (1000) 6 | #define DEBOUNCE_TIME (40) 7 | 8 | struct Button; 9 | struct MicrowaveEmitter; 10 | struct Timer; 11 | 12 | typedef struct ButtonDriver ButtonDriver; 13 | struct ButtonDriver { 14 | unsigned char oldState; 15 | unsigned char toggleOn; 16 | struct Button* itsButton; 17 | struct MicrowaveEmitter* itsMicrowaveEmitter; 18 | struct Timer* itsTimer; 19 | }; 20 | 21 | 22 | void ButtonDriver_Init(ButtonDriver* const me); 23 | void ButtonDriver_Cleanup(ButtonDriver* const me); 24 | 25 | /* Operations */ 26 | void ButtonDriver_eventReceive(ButtonDriver* const me); 27 | struct Button* ButtonDriver_getItsButton(const ButtonDriver* const me); 28 | void ButtonDriver_setItsButton(ButtonDriver* const me, struct Button* p_Button); 29 | 30 | struct MicrowaveEmitter* ButtonDriver_getItsMicrowaveEmitter(const ButtonDriver* const me); 31 | void ButtonDriver_setItsMicrowaveEmitter(ButtonDriver* const me, struct MicrowaveEmitter* p_MicrowaveEmitter); 32 | 33 | struct Timer* ButtonDriver_getItsTimer(const ButtonDriver* const me); 34 | void ButtonDriver_setItsTimer(ButtonDriver* const me, struct Timer* p_Timer); 35 | 36 | ButtonDriver * ButtonDriver_Create(void); 37 | void ButtonDriver_Destroy(ButtonDriver* const me); 38 | 39 | void ButtonDriver_setItsButton2(ButtonDriver* const me, struct Button* p_Button); 40 | void ButtonDriver_setItsButton1(ButtonDriver* const me, struct Button* p_Button); 41 | void ButtonDriver_clearItsButton(ButtonDriver* const me); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /chapter4/05-queue/SensorThread.c: -------------------------------------------------------------------------------- 1 | #include "SensorThread.h" 2 | /* 3 | updateData runs every period of the sensor thread 4 | and calls the getXXData() function of each of 5 | its sensors, then uses a random number generator 6 | to decide which data should be published to the 7 | GasDataQueue. 8 | */ 9 | void SensorThread_updateData(SensorThread* const me) { 10 | unsigned char success; 11 | GasData g; 12 | 13 | GasData_Init(&g); 14 | 15 | O2Sensor_getO2Data(&(me->itsO2Sensor)); 16 | N2Sensor_getN2Data(&(me->itsN2Sensor)); 17 | HeSensor_getHeData(&(me->itsHeSensor)); 18 | 19 | if (rand() > RAND_MAX / 3) { 20 | g.gType = HE_GAS; 21 | g.conc = me->itsHeSensor.conc; 22 | g.flowInCCPerMin = me->itsHeSensor.flow; 23 | success = GasDataQueue_insert(me->itsGasDataQueue, g); 24 | if (!success) 25 | printf("Helium Gas Data queue insertion failed!\n"); 26 | }; 27 | 28 | if (rand() > RAND_MAX / 3) { 29 | g.gType = N2_GAS; 30 | g.conc = me->itsN2Sensor.conc; 31 | g.flowInCCPerMin = me->itsN2Sensor.flow; 32 | success = GasDataQueue_insert(me->itsGasDataQueue, g); 33 | if (!success) 34 | printf("Nitrogen Gas Data queue insertion failed!\n"); 35 | }; 36 | 37 | if (rand() > RAND_MAX / 3) { 38 | g.gType = O2_GAS; 39 | g.conc = me->itsO2Sensor.conc; 40 | g.flowInCCPerMin = me->itsO2Sensor.flow; 41 | success = GasDataQueue_insert(me->itsGasDataQueue, g); 42 | if (!success) 43 | printf("Oxygen Gas Data queue insertion failed! \n"); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /chapter6/04-channelmode/EMGChannel.h: -------------------------------------------------------------------------------- 1 | #ifndef EMGChannel_H 2 | #define EMGChannel_H 3 | 4 | #include "CalculateColor.h" 5 | #include "ConvertToFrequency.h" 6 | #include "EMGSensorDeviceDriver.h" 7 | #include "LightDeviceDriver.h" 8 | #include "MovingAverageFilter.h" 9 | 10 | typedef struct EMGChannel EMGChannel; 11 | struct EMGChannel { 12 | struct CalculateColor itsCalculateColor; 13 | struct ConvertToFrequency itsConvertToFrequency; 14 | struct EMGSensorDeviceDriver itsEMGSensorDeviceDriver; 15 | struct LightDeviceDriver itsLightDeviceDriver; 16 | struct MovingAverageFilter itsMovingAverageFilter; 17 | }; 18 | 19 | /* Constructors and destructors:*/ 20 | void EMGChannel_Init(EMGChannel* const me); 21 | void EMGChannel_Cleanup(EMGChannel* const me); 22 | 23 | /* Operations */ 24 | void EMGChannel_acquireData(EMGChannel* const me); 25 | double EMGChannel_getFrequency(EMGChannel* const me); 26 | long EMGChannel_getLightColor(EMGChannel* const me); 27 | int EMGChannel_getVoltage(EMGChannel* const me); 28 | void EMGChannel_setSensitivity(EMGChannel* const me, int sen); 29 | 30 | struct CalculateColor* EMGChannel_getItsCalculateColor(const EMGChannel* const me); 31 | struct ConvertToFrequency* EMGChannel_getItsConvertToFrequency(const EMGChannel* const me); 32 | struct EMGSensorDeviceDriver* EMGChannel_getItsEMGSensorDeviceDriver(const EMGChannel* const me); 33 | struct LightDeviceDriver* EMGChannel_getItsLightDeviceDriver(const EMGChannel* const me); 34 | struct MovingAverageFilter* EMGChannel_getItsMovingAverageFilter(const EMGChannel* const me); 35 | 36 | EMGChannel * EMGChannel_Create(void); 37 | void EMGChannel_Destroy(EMGChannel* const me); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /chapter6/05-protectsinglechannel/CheckTemperature.c: -------------------------------------------------------------------------------- 1 | #include "CheckTemperature.h" 2 | #include "FurnaceController.h" 3 | 4 | static void cleanUpRelations(CheckTemperature* const me); 5 | 6 | void CheckTemperature_Init(CheckTemperature* const me) { 7 | me->maximumTemp = 130; 8 | me->minimumTemp = 0; 9 | me->itsFurnaceController = NULL; 10 | } 11 | 12 | void CheckTemperature_Cleanup(CheckTemperature* const me) { 13 | cleanUpRelations(me); 14 | } 15 | 16 | void CheckTemperature_checkTemperature(CheckTemperature* const me, int temp) { 17 | if (temp < me->minimumTemp) 18 | FurnaceController_emergencyRestart(me->itsFurnaceController); 19 | if (temp > me->maximumTemp) 20 | FurnaceController_shutDown(me->itsFurnaceController); 21 | } 22 | 23 | struct FurnaceController* CheckTemperature_getItsFurnaceController(const CheckTemperature* const me) { 24 | return (struct FurnaceController*)me->itsFurnaceController; 25 | } 26 | 27 | void CheckTemperature_setItsFurnaceController(CheckTemperature* const me, struct FurnaceController* p_FurnaceController) { 28 | me->itsFurnaceController = p_FurnaceController; 29 | } 30 | 31 | CheckTemperature * CheckTemperature_Create(void) { 32 | CheckTemperature* me = (CheckTemperature *) malloc(sizeof(CheckTemperature)); 33 | if(me!=NULL) 34 | CheckTemperature_Init(me); 35 | return me; 36 | } 37 | 38 | void CheckTemperature_Destroy(CheckTemperature* const me) { 39 | if(me!=NULL) 40 | CheckTemperature_Cleanup(me); 41 | free(me); 42 | } 43 | 44 | static void cleanUpRelations(CheckTemperature* const me) { 45 | if(me->itsFurnaceController != NULL) 46 | me->itsFurnaceController = NULL; 47 | } 48 | -------------------------------------------------------------------------------- /chapter2/01-client-server/QRSDetector.c: -------------------------------------------------------------------------------- 1 | #include "QRSDetector.h" 2 | #include "TMDQueue.h" 3 | #include 4 | 5 | static void cleanUpRelations(QRSDetector* const me); 6 | 7 | void QRSDetector_Init(QRSDetector* const me){ 8 | me->heartRate = 0; 9 | me->index = 0; 10 | me->itsTMDQueue = NULL; 11 | } 12 | 13 | void QRSDetector_Cleanup(QRSDetector* const me){ 14 | cleanUpRelations(me); 15 | } 16 | 17 | void QRSDetector_computeHR(QRSDetector* const me){ 18 | me->heartRate = me->heartRate + 10; 19 | } 20 | 21 | void QRSDetector_getDataSample(QRSDetector* const me){ 22 | TimeMarkedData tmd; 23 | tmd = TMDQueue_remove(me->itsTMDQueue, me->index); 24 | printf("QRSDetector index: %d TimeInterval: %d DataValue: %d\n", me->index, tmd.timeInterval, tmd.dataValue); 25 | me->index = TMDQueue_getNextIndex(me->itsTMDQueue, me->index); 26 | } 27 | 28 | void QRSDetector_setItsTMDQueue(QRSDetector* const me, struct TMDQueue* p_TMDQueue){ 29 | me->itsTMDQueue = p_TMDQueue; 30 | } 31 | 32 | struct TMDQueue* QRSDetector_getItsTMDQueue(const QRSDetector* const me){ 33 | return (struct TMDQueue*)me->itsTMDQueue; 34 | } 35 | 36 | QRSDetector * QRSDetector_Create(void){ 37 | QRSDetector* me = (QRSDetector*)malloc(sizeof(QRSDetector)); 38 | if (me != NULL) 39 | { 40 | QRSDetector_Init(me); 41 | } 42 | return me; 43 | } 44 | 45 | QRSDetector * QRSDetector_Destroy(QRSDetector* const me){ 46 | if (me != NULL) 47 | { 48 | QRSDetector_Cleanup(me); 49 | } 50 | free(me); 51 | } 52 | 53 | static void cleanUpRelations(QRSDetector* const me) { 54 | if(me->itsTMDQueue != NULL) 55 | { 56 | me->itsTMDQueue = NULL; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /chapter4/02-staticpriority/MotorController.c: -------------------------------------------------------------------------------- 1 | #include "MotorData.h" 2 | #include 3 | #include "MotorController.h" 4 | 5 | static int mc_priority = 8; 6 | static int mc_eventQueue[10]; 7 | static int mc_stack[1024]; 8 | 9 | int motor1Pos; 10 | int motor2Pos; 11 | 12 | void MotorController_Init(void) { 13 | /* use a queue of 10 events, and a 1024 element stack */ 14 | /* create the OS event queue and the task */ 15 | OSQCreate (&mc_eventQueue[0], 10); 16 | OSTaskCreate (MotorController_run, NULL, (void *)&mc_stack[1024], 5); 17 | 18 | /* now run the task */ 19 | MotorController_run(); 20 | } 21 | 22 | void MotorController_Cleanup(void) { 23 | } 24 | 25 | void MotorController_run(void) { 26 | int os_pend_timer_id, x, y; 27 | os_pend_timer_id = OSCreateTimer(100, RETRIGGER); 28 | 29 | while (TRUE) { 30 | OSQPend(os_pend_timer_id, WAIT_FOREVER);; 31 | move(); 32 | }; 33 | } 34 | 35 | void move(void) { 36 | int m1Pos, m2Pos; 37 | /* this function would read the instrument 38 | panel to set the motor movement settings 39 | to set the position of the motors. 40 | Note that if you want to set only one 41 | of the motors, then set a negative value 42 | for the commanded position of the other 43 | */ 44 | m1Pos = rand(); 45 | m2Pos = rand(); 46 | 47 | if (m1Pos >= 0) 48 | motor1Pos = m1Pos; 49 | if (m2Pos >=0) 50 | motor2Pos = m2Pos; 51 | setCmdPos(100*m1Pos + m2Pos); 52 | } 53 | 54 | void zero(void) { 55 | motor1Pos = 0; 56 | motor2Pos = 0; 57 | setCmdPos(0); 58 | } 59 | 60 | void MotorController_initRelations(void) { 61 | MotorController_Init(); 62 | } 63 | -------------------------------------------------------------------------------- /chapter5/03-StateTable/StateTablePattern.h: -------------------------------------------------------------------------------- 1 | #ifndef StateTablePattern_H 2 | #define StateTablePattern_H 3 | 4 | typedef enum TSTATETYPE { 5 | NULL_STATE, 6 | NONUMBER_STATE, 7 | GN_PROCESSINGWHOLEPART_STATE, 8 | GN_PROCESSINGFRACTIONALPART_STATE 9 | } TSTATETYPE; 10 | 11 | /* This action pointer accepts the me object pointer and no other parameters. */ 12 | typedef void (*ActionPtr0)(void*); 13 | 14 | /* This action pointer accepts the me object pointer and the passed character. This means that the client must always send the character data with the event. */ 15 | typedef void (*ActionPtr1)(void*, char); 16 | 17 | typedef enum EventType { 18 | EVDIGIT, 19 | EVDOT, 20 | EVWHITESPACE, 21 | EVENDOFSTRING 22 | } EventType; 23 | 24 | typedef struct { 25 | EventType eType; 26 | union eventData { 27 | char c; 28 | } ed; 29 | } Event; 30 | 31 | typedef struct ap { 32 | int nParams; /* 0 or 1 in this case */ 33 | union { 34 | ActionPtr0 a0; 35 | ActionPtr1 a1; 36 | } aPtr; 37 | } ActionType; 38 | 39 | typedef ActionType* ActionPtr; 40 | /* This function returns a int that is treated as a boolean (0 == FALSE) and, if present, is used to determine whether or not the transition is taken. Note that this function takes no paramaeters (other than the object me pointer). */ 41 | typedef int (*GuardType)(void *); 42 | 43 | 44 | typedef struct TableEntryType { 45 | ActionPtr entryActionPtr; 46 | ActionPtr exitActionPtr; 47 | GuardType guardPtr; 48 | TSTATETYPE newState; 49 | ActionPtr transActionPtr; 50 | } TableEntryType; 51 | 52 | /* digit returns an int value of the char 53 | That is: return c-'0' */ 54 | int digit(char c); 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /chapter5/04-StatePattern/GN_ProcessingFractionState.h: -------------------------------------------------------------------------------- 1 | #ifndef GN_ProcessingFractionState_H 2 | #define GN_ProcessingFractionState_H 3 | 4 | #include "StatePattern.h" 5 | #include "StateActionSet.h" 6 | 7 | struct Context; 8 | 9 | typedef struct GN_ProcessingFractionState GN_ProcessingFractionState; 10 | struct GN_ProcessingFractionState { 11 | struct StateActionSet aSet; 12 | struct Context* itsContext; 13 | }; 14 | 15 | /*## operation Init() */ 16 | void GN_ProcessingFractionState_Init(GN_ProcessingFractionState* const me); 17 | void GN_ProcessingFractionState_Cleanup(GN_ProcessingFractionState* const me); 18 | 19 | /* Operations */ 20 | 21 | void GN_ProcessingFractionState_Frac2Frac(GN_ProcessingFractionState* const me, char c); 22 | void GN_ProcessingFractionState_entryAction(GN_ProcessingFractionState* const me); 23 | int GN_ProcessingFractionState_evDigit(GN_ProcessingFractionState* const me, char c); 24 | int GN_ProcessingFractionState_evDot(GN_ProcessingFractionState* const me); 25 | int GN_ProcessingFractionState_evEndOfString(GN_ProcessingFractionState* const me); 26 | int GN_ProcessingFractionState_evWhiteSpace(GN_ProcessingFractionState* const me); 27 | void GN_ProcessingFractionState_exitAction(GN_ProcessingFractionState* const me); 28 | void GN_ProcessingFractionState_printResult(GN_ProcessingFractionState* const me); 29 | 30 | struct Context* GN_ProcessingFractionState_getItsContext(const GN_ProcessingFractionState* const me); 31 | 32 | 33 | void GN_ProcessingFractionState_setItsContext(GN_ProcessingFractionState* const me, struct Context* p_Context); 34 | 35 | GN_ProcessingFractionState * GN_ProcessingFractionState_Create(void); 36 | 37 | void GN_ProcessingFractionState_Destroy(GN_ProcessingFractionState* const me); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /chapter4/04-guard/FlightDataDisplay.c: -------------------------------------------------------------------------------- 1 | #include "FlightDataDisplay.h" 2 | #include "KinematicData.h" 3 | #include 4 | 5 | static void cleanUpRelations(FlightDataDisplay* const me); 6 | 7 | void FlightDataDisplay_Init(FlightDataDisplay* const me) { 8 | me->itsKinematicData = NULL; 9 | } 10 | 11 | void FlightDataDisplay_Cleanup(FlightDataDisplay* const me) { 12 | cleanUpRelations(me); 13 | } 14 | 15 | void FlightDataDisplay_showFlightData(FlightDataDisplay* const me) { 16 | Attitude a; 17 | Position p; 18 | a = KinematicData_getAttitude(me->itsKinematicData); 19 | p = KinematicData_getPosition(me->itsKinematicData); 20 | printf("Roll, pitch, yaw = %f, %f, %f \n", a.roll, a.pitch, a.yaw); 21 | printf("Lat, Long, Altitude = %f, %f, %f\n", p.latitude, p.longitude, p.altitude); 22 | } 23 | 24 | struct KinematicData* FlightDataDisplay_getItsKinematicData(const FlightDataDisplay* const me) { 25 | return (struct KinematicData*)me->itsKinematicData; 26 | } 27 | 28 | void FlightDataDisplay_setItsKinematicData(FlightDataDisplay* const me, struct KinematicData* p_KinematicData) { 29 | me->itsKinematicData = p_KinematicData; 30 | } 31 | 32 | FlightDataDisplay * FlightDataDisplay_Create(void) { 33 | FlightDataDisplay* me = (FlightDataDisplay *) malloc(sizeof(FlightDataDisplay)); 34 | if(me!=NULL) { 35 | FlightDataDisplay_Init(me); 36 | } 37 | return me; 38 | } 39 | 40 | void FlightDataDisplay_Destroy(FlightDataDisplay* const me) { 41 | if(me!=NULL) { 42 | FlightDataDisplay_Cleanup(me); 43 | } 44 | free(me); 45 | } 46 | 47 | static void cleanUpRelations(FlightDataDisplay* const me) { 48 | if(me->itsKinematicData != NULL) { 49 | me->itsKinematicData = NULL; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /chapter6/03-SmartData/SmartInt.h: -------------------------------------------------------------------------------- 1 | #ifndef SmartInt_H 2 | #define SmartInt_H 3 | 4 | #include "SmartDataExample.h" 5 | struct AlarmManager; 6 | 7 | typedef struct SmartInt SmartInt; 8 | struct SmartInt { 9 | ErrorCodeType errorCode; 10 | int highRange; 11 | int lowRange; 12 | int value; 13 | struct AlarmManager* itsAlarmManager; 14 | }; 15 | 16 | /* Constructors and destructors:*/ 17 | void SmartInt_Init(SmartInt* const me, int val, int low, int high, struct AlarmManager* errMgr); 18 | 19 | void SmartInt_Cleanup(SmartInt* const me); 20 | 21 | /* Operations */ 22 | ErrorCodeType SmartInt_checkValidity(SmartInt* const me); 23 | 24 | int SmartInt_cmp(SmartInt* const me, SmartInt s); 25 | 26 | void SmartInt_errorHandler(SmartInt* const me, ErrorCodeType err); 27 | 28 | ErrorCodeType SmartInt_getErrorCode(SmartInt* const me); 29 | 30 | int SmartInt_getHighBoundary(SmartInt* const me); 31 | 32 | int SmartInt_getLowBoundary(SmartInt* const me); 33 | 34 | int SmartInt_getPrimitive(SmartInt* const me); 35 | 36 | SmartInt SmartInt_getValue(SmartInt* const me); 37 | 38 | int SmartInt_pCmp(SmartInt* const me, SmartInt s); 39 | 40 | void SmartInt_setHighBoundary(SmartInt* const me, int high); 41 | 42 | void SmartInt_setLowBoundary(SmartInt* const me, int low); 43 | 44 | ErrorCodeType SmartInt_setPrimitive(SmartInt* const me, int p); 45 | 46 | ErrorCodeType SmartInt_setValue(SmartInt* const me, SmartInt s); 47 | 48 | struct AlarmManager* SmartInt_getItsAlarmManager(const SmartInt* const me); 49 | 50 | void SmartInt_setItsAlarmManager(SmartInt* const me, struct AlarmManager* p_AlarmManager); 51 | 52 | SmartInt * SmartInt_Create(int val, int low, int high, struct AlarmManager* errMgr); 53 | 54 | void SmartInt_Destroy(SmartInt* const me); 55 | #endif 56 | -------------------------------------------------------------------------------- /chapter2/02-observer/ECG_Module.c: -------------------------------------------------------------------------------- 1 | #include "ECG_Module.h" 2 | #include "TMDQueue.h" 3 | #include 4 | 5 | static void cleanUpRelations(ECG_Module* const me); 6 | 7 | void ECG_Module_Init(ECG_Module* const me) { 8 | me->dataNum = 0; 9 | me->itsTMDQueue = NULL; 10 | } 11 | 12 | void ECG_Module_Cleanup(ECG_Module* const me) { 13 | cleanUpRelations(me); 14 | } 15 | 16 | /* operation acquireValue() */ 17 | void ECG_Module_acquireValue(ECG_Module* const me) { 18 | // in actual implemetnation, this would return the 19 | // measured voltage across the lead pair 20 | TimeMarkedData tmd; 21 | tmd.dataValue = rand(); 22 | tmd.timeInterval = ++me->dataNum; 23 | TMDQueue_insert(me->itsTMDQueue, tmd); 24 | } 25 | 26 | /* operation setLeadPair(int,int) */ 27 | void ECG_Module_setLeadPair(ECG_Module* const me, int l1, int l2) { 28 | me->lead1 = l1; 29 | me->lead2 = l2; 30 | } 31 | 32 | struct TMDQueue* ECG_Module_getItsTMDQueue(const ECG_Module* const me) { 33 | return (struct TMDQueue*)me->itsTMDQueue; 34 | } 35 | 36 | void ECG_Module_setItsTMDQueue(ECG_Module* const me, struct TMDQueue* p_TMDQueue) { 37 | me->itsTMDQueue = p_TMDQueue; 38 | } 39 | 40 | ECG_Module * ECG_Module_Create(void) { 41 | ECG_Module* me = (ECG_Module *) malloc(sizeof(ECG_Module)); 42 | if(me!=NULL) 43 | { 44 | ECG_Module_Init(me); 45 | } 46 | return me; 47 | } 48 | 49 | void ECG_Module_Destroy(ECG_Module* const me) { 50 | if(me!=NULL) 51 | { 52 | ECG_Module_Cleanup(me); 53 | } 54 | free(me); 55 | } 56 | 57 | static void cleanUpRelations(ECG_Module* const me) { 58 | if(me->itsTMDQueue != NULL) 59 | { 60 | me->itsTMDQueue = NULL; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /chapter2/01-client-server/ECG_Module.c: -------------------------------------------------------------------------------- 1 | #include "ECG_Module.h" 2 | #include "TMDQueue.h" 3 | #include 4 | 5 | static void cleanUpRelations(ECG_Module* const me); 6 | 7 | void ECG_Module_Init(ECG_Module* const me) { 8 | me->dataNum = 0; 9 | me->itsTMDQueue = NULL; 10 | } 11 | 12 | void ECG_Module_Cleanup(ECG_Module* const me) { 13 | cleanUpRelations(me); 14 | } 15 | 16 | /* operation acquireValue() */ 17 | void ECG_Module_acquireValue(ECG_Module* const me) { 18 | // in actual implemetnation, this would return the 19 | // measured voltage across the lead pair 20 | TimeMarkedData tmd; 21 | tmd.dataValue = rand(); 22 | tmd.timeInterval = ++me->dataNum; 23 | TMDQueue_insert(me->itsTMDQueue, tmd); 24 | } 25 | 26 | /* operation setLeadPair(int,int) */ 27 | void ECG_Module_setLeadPair(ECG_Module* const me, int l1, int l2) { 28 | me->lead1 = l1; 29 | me->lead2 = l2; 30 | } 31 | 32 | struct TMDQueue* ECG_Module_getItsTMDQueue(const ECG_Module* const me) { 33 | return (struct TMDQueue*)me->itsTMDQueue; 34 | } 35 | 36 | void ECG_Module_setItsTMDQueue(ECG_Module* const me, struct TMDQueue* p_TMDQueue) { 37 | me->itsTMDQueue = p_TMDQueue; 38 | } 39 | 40 | ECG_Module * ECG_Module_Create(void) { 41 | ECG_Module* me = (ECG_Module *) malloc(sizeof(ECG_Module)); 42 | if(me!=NULL) 43 | { 44 | ECG_Module_Init(me); 45 | } 46 | return me; 47 | } 48 | 49 | void ECG_Module_Destroy(ECG_Module* const me) { 50 | if(me!=NULL) 51 | { 52 | ECG_Module_Cleanup(me); 53 | } 54 | free(me); 55 | } 56 | 57 | static void cleanUpRelations(ECG_Module* const me) { 58 | if(me->itsTMDQueue != NULL) 59 | { 60 | me->itsTMDQueue = NULL; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /chapter3/01-hardwareproxy/maintest.c: -------------------------------------------------------------------------------- 1 | #include "MotorProxy.h" 2 | #include 3 | 4 | int main(int argc, char const *argv[]) 5 | { 6 | /* code */ 7 | MotorProxy * motor = MotorProxy_Create(); 8 | // config rotaryArmLength and motor address 9 | unsigned int rotaryArmLength = 10; 10 | unsigned int location = 0x7ffc396a2730; 11 | unsigned int motorData = 100; 12 | 13 | DirectionType direction; 14 | unsigned int speed; 15 | 16 | MotorProxy_configure(motor, rotaryArmLength, &location, &motorData); 17 | MotorProxy_initialize(motor); 18 | MotorProxy_enable(motor); 19 | 20 | direction = FORWARD; 21 | speed = 10; 22 | MotorProxy_writeMotorSpeed(motor, direction, speed); 23 | DirectionType getdirection = MotorProxy_accessMotorDirection(motor); 24 | unsigned int getspeed = MotorProxy_accessMotorSpeed(motor); 25 | unsigned int getstate = MotorProxy_aceessMotorState(motor); 26 | printf("Motor current direction:%d, speed:%d, state:%d\n", getdirection, getspeed, getstate); 27 | 28 | direction = REVERSE; 29 | speed = 20; 30 | MotorProxy_writeMotorSpeed(motor, direction, speed); 31 | getdirection = MotorProxy_accessMotorDirection(motor); 32 | getspeed = MotorProxy_accessMotorSpeed(motor); 33 | getstate = MotorProxy_aceessMotorState(motor); 34 | printf("Motor current direction:%d, speed:%d, state:%d\n", getdirection, getspeed, getstate); 35 | 36 | MotorProxy_disable(motor); 37 | getdirection = MotorProxy_accessMotorDirection(motor); 38 | getspeed = MotorProxy_accessMotorSpeed(motor); 39 | getstate = MotorProxy_aceessMotorState(motor); 40 | printf("Motor current direction:%d, speed:%d, state:%d\n", getdirection, getspeed, getstate); 41 | 42 | MotorProxy_Destroy(motor); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /chapter6/03-SmartData/PatientDataClass.h: -------------------------------------------------------------------------------- 1 | #ifndef PatientDataClass_H 2 | #define PatientDataClass_H 3 | 4 | #include "SmartDataExample.h" 5 | #include "AlarmManager.h" 6 | #include "SmartColor.h" 7 | #include "SmartInt.h" 8 | 9 | typedef struct PatientDataClass PatientDataClass; 10 | struct PatientDataClass { 11 | SmartInt age; 12 | SmartColor backgroundColor; 13 | SmartColor foregroundColor; 14 | SmartInt heartRate; 15 | char name[100]; 16 | long patientID; 17 | SmartInt weight; 18 | }; 19 | 20 | /* Constructors and destructors:*/ 21 | void PatientDataClass_Init(PatientDataClass* const me, AlarmManager* errMgr); 22 | void PatientDataClass_Cleanup(PatientDataClass* const me); 23 | 24 | /* Operations */ 25 | ErrorCodeType PatientDataClass_checkAllData(PatientDataClass* const me); 26 | 27 | int PatientDataClass_getAge(PatientDataClass* const me); 28 | ColorType PatientDataClass_getBColor(PatientDataClass* const me); 29 | ColorType PatientDataClass_getFColor(PatientDataClass* const me); 30 | int PatientDataClass_getHeartRate(PatientDataClass* const me); 31 | char* PatientDataClass_getName(PatientDataClass* const me); 32 | int PatientDataClass_getWeight(PatientDataClass* const me); 33 | 34 | void PatientDataClass_setAge(PatientDataClass* const me, int a); 35 | void PatientDataClass_setBColor(PatientDataClass* const me, ColorType bc); 36 | void PatientDataClass_setFColor(PatientDataClass* const me, ColorType fc); 37 | void PatientDataClass_setHeartRate(PatientDataClass* const me, int hr); 38 | void PatientDataClass_setName(PatientDataClass* const me, char* n); 39 | void PatientDataClass_setWeight(PatientDataClass* const me, int w); 40 | 41 | PatientDataClass * PatientDataClass_Create(AlarmManager* errMgr); 42 | void PatientDataClass_Destroy(PatientDataClass* const me); 43 | #endif 44 | -------------------------------------------------------------------------------- /chapter3/04-observer/GasSensor.h: -------------------------------------------------------------------------------- 1 | #ifndef GasSensor_H 2 | #define GasSensor_H 3 | 4 | #include "GasData.h" 5 | #include "GasNotificationHandle.h" 6 | #include 7 | #include 8 | 9 | /* the function pointer type 10 | The first value of the function pointer is to the instance 11 | data of the class. The second is a ptr to the new gas data 12 | */ 13 | typedef void (*gasDataAcceptorPtr)(void *, struct GasData*); 14 | 15 | struct GasNotificationHandle; 16 | 17 | /* class GasSensor */ 18 | typedef struct GasSensor GasSensor; 19 | struct GasSensor { 20 | struct GasData* itsGasData; 21 | struct GasNotificationHandle *itsGasNH[100]; 22 | }; 23 | 24 | /* Constructors and destructors:*/ 25 | void GasSensor_Init(GasSensor* const me); 26 | void GasSensor_Cleanup(GasSensor* const me); 27 | 28 | 29 | /* Operations */ 30 | void GasSensor_dumpList(GasSensor* const me); 31 | void GasSensor_newData(GasSensor* const me, unsigned int flow, unsigned int n2, unsigned int o2); 32 | void GasSensor_notify(GasSensor* const me); 33 | void GasSensor_subscribe(GasSensor* const me, void * instancePtr, const gasDataAcceptorPtr* aPtr); 34 | void GasSensor_unsubscribe(GasSensor* const me, const gasDataAcceptorPtr* aPtr); 35 | struct GasData* GasSensor_getItsGasData(const GasSensor* const me); 36 | void GasSensor_setItsGasData(GasSensor* const me, struct GasData* p_GasData); 37 | int GasSensor_getItsGasNH(const GasSensor* const me); 38 | void GasSensor_addItsGasNH(GasSensor* const me, struct GasNotificationHandle * p_GasNotificationHandle); 39 | void GasSensor_removeItsGasNH(GasSensor* const me, struct GasNotificationHandle * p_GasNotificationHandle); 40 | void GasSensor_clearItsGasNH(GasSensor* const me); 41 | GasSensor * GasSensor_Create(void); 42 | void GasSensor_Destroy(GasSensor* const me); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /chapter2/02-observer/HistogramDisplay.c: -------------------------------------------------------------------------------- 1 | #include "HistogramDisplay.h" 2 | #include "TimeMarkedData.h" 3 | #include "TMDQueue.h" 4 | 5 | static void cleanUpRelations(HistogramDisplay* const me); 6 | 7 | void HistogramDisplay_Init(HistogramDisplay* const me) { 8 | me->itsTMDQueue = NULL; 9 | { 10 | /* call subscribe to connect to the server */ 11 | TMDQueue_subscribe(me->itsTMDQueue, HistogramDisplay_update); 12 | } 13 | } 14 | 15 | void HistogramDisplay_Cleanup(HistogramDisplay* const me) { 16 | /* remove yourself from server subscription list */ 17 | TMDQueue_unsubscribe(me->itsTMDQueue, HistogramDisplay_update); 18 | cleanUpRelations(me); 19 | } 20 | 21 | void HistogramDisplay_update(HistogramDisplay* const me, const struct TimeMarkedData tmd) { 22 | printf(" Histogram -> TimeInterval: %d DataValue: %d\n", tmd.timeInterval, tmd.dataValue); 23 | } 24 | 25 | void HistogramDisplay_updateHistogram(HistogramDisplay* const me) { 26 | 27 | } 28 | 29 | struct TMDQueue* HistogramDisplay_getItsTMDQueue(const HistogramDisplay* const me) { 30 | return (struct TMDQueue*)me->itsTMDQueue; 31 | } 32 | 33 | void HistogramDisplay_setItsTMDQueue(HistogramDisplay* const me, struct TMDQueue* p_TMDQueue) { 34 | me->itsTMDQueue = p_TMDQueue; 35 | } 36 | 37 | HistogramDisplay * HistogramDisplay_Create(void) { 38 | HistogramDisplay* me = (HistogramDisplay *) malloc(sizeof(HistogramDisplay)); 39 | if(me!=NULL) 40 | { 41 | HistogramDisplay_Init(me); 42 | } 43 | return me; 44 | } 45 | 46 | void HistogramDisplay_Destroy(HistogramDisplay* const me) { 47 | if(me!=NULL) 48 | { 49 | HistogramDisplay_Cleanup(me); 50 | } 51 | free(me); 52 | } 53 | 54 | static void cleanUpRelations(HistogramDisplay* const me) { 55 | if(me->itsTMDQueue != NULL) 56 | { 57 | me->itsTMDQueue = NULL; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /chapter6/03-SmartData/SmartColor.h: -------------------------------------------------------------------------------- 1 | #ifndef SmartColor_H 2 | #define SmartColor_H 3 | 4 | #include "SmartDataExample.h" 5 | #include "SmartInt.h" 6 | 7 | struct AlarmManager; 8 | 9 | typedef struct SmartColor SmartColor; 10 | struct SmartColor { 11 | ErrorCodeType errorCode; 12 | ColorType highRange; 13 | ColorType lowRange; 14 | ColorType value; 15 | struct AlarmManager* itsAlarmManager; 16 | }; 17 | /* Constructors and destructors:*/ 18 | 19 | void SmartColor_Init(SmartColor* const me, ColorType val, ColorType low, ColorType high, struct AlarmManager* errMgr); 20 | void SmartColor_Cleanup(SmartColor* const me); 21 | 22 | /* Operations */ 23 | ErrorCodeType SmartColor_checkValidity(SmartColor* const me); 24 | int SmartColor_cmp(SmartColor* const me, SmartColor s); 25 | void SmartColor_errorHandler(SmartColor* const me, ErrorCodeType err); 26 | int SmartColor_pCmp(SmartColor* const me, SmartInt s); 27 | 28 | 29 | ErrorCodeType SmartColor_getErrorCode(SmartColor* const me); 30 | ColorType SmartColor_getHighBoundary(SmartColor* const me); 31 | ColorType SmartColor_getLowBoundary(SmartColor* const me); 32 | ColorType SmartColor_getPrimitive(SmartColor* const me); 33 | SmartColor SmartColor_getValue(SmartColor* const me); 34 | 35 | void SmartColor_setHighBoundary(SmartColor* const me, ColorType high); 36 | void SmartColor_setLowBoundary(SmartColor* const me, ColorType low); 37 | ErrorCodeType SmartColor_setPrimitive(SmartColor* const me, ColorType p); 38 | ErrorCodeType SmartColor_setValue(SmartColor* const me, SmartColor s); 39 | 40 | struct AlarmManager* SmartColor_getItsAlarmManager(const SmartColor* const me); 41 | void SmartColor_setItsAlarmManager(SmartColor* const me, struct AlarmManager* p_AlarmManager); 42 | 43 | SmartColor * SmartColor_Create(ColorType val, ColorType low, ColorType high, struct AlarmManager* errMgr); 44 | void SmartColor_Destroy(SmartColor* const me); 45 | #endif 46 | -------------------------------------------------------------------------------- /chapter4/04-guard/KinematicData.c: -------------------------------------------------------------------------------- 1 | #include "KinematicData.h" 2 | 3 | void KinematicData_Init(KinematicData* const me) { 4 | 5 | Attitude_Init(&(me->attitude)); 6 | Position_Init(&(me->position)); 7 | me->sema = OS_create_semaphore(); 8 | } 9 | 10 | void KinematicData_Cleanup(KinematicData* const me) { 11 | OS_destroy_semaphore(me->sema); 12 | } 13 | 14 | struct Position KinematicData_getPosition(KinematicData* const me) { 15 | Position p; 16 | 17 | /* engage the lock */ 18 | OS_lock_semaphore(me->sema); 19 | 20 | p = me->position; 21 | 22 | /* release the lock */ 23 | OS_release_semaphore(me->sema); 24 | 25 | return p; 26 | } 27 | 28 | void KinematicData_setAttitude(KinematicData* const me, Attitude a) { 29 | /* engage the lock */ 30 | OS_lock_semaphore(me->sema); 31 | 32 | me->attitude = a; 33 | 34 | /* release the lock */ 35 | OS_release_semaphore(me->sema); 36 | } 37 | 38 | void KinematicData_setPosition(KinematicData* const me, Position p) { 39 | /* engage the lock */ 40 | OS_lock_semaphore(me->sema); 41 | 42 | me->position = p; 43 | 44 | /* release the lock */ 45 | OS_release_semaphore(me->sema); 46 | 47 | /*#]*/ 48 | } 49 | 50 | KinematicData * KinematicData_Create(void) { 51 | KinematicData* me = (KinematicData *) malloc(sizeof(KinematicData)); 52 | if(me!=NULL) { 53 | KinematicData_Init(me); 54 | } 55 | return me; 56 | } 57 | 58 | void KinematicData_Destroy(KinematicData* const me) { 59 | if(me!=NULL) { 60 | KinematicData_Cleanup(me); 61 | } 62 | free(me); 63 | } 64 | 65 | Attitude KinematicData_getAttitude(KinematicData* const me) { 66 | Attitude a; 67 | 68 | /* engage the lock */ 69 | OS_lock_semaphore(me->sema); 70 | 71 | a = me->attitude; 72 | 73 | /* release the lock */ 74 | OS_release_semaphore(me->sema); 75 | 76 | return a; 77 | } 78 | -------------------------------------------------------------------------------- /chapter2/01-client-server/HistogramDisplay.c: -------------------------------------------------------------------------------- 1 | #include "HistogramDisplay.h" 2 | #include "TMDQueue.h" 3 | #include 4 | 5 | static void cleanUpRelations(HistogramDisplay* const me); 6 | 7 | void HistogramDisplay_Init(HistogramDisplay* const me) { 8 | me->index = 0; 9 | me->itsTMDQueue = NULL; 10 | } 11 | 12 | void HistogramDisplay_Cleanup(HistogramDisplay* const me) { 13 | cleanUpRelations(me); 14 | } 15 | 16 | /* operation getValue() */ 17 | void HistogramDisplay_getValue(HistogramDisplay* const me) { 18 | TimeMarkedData tmd; 19 | tmd = TMDQueue_remove(me->itsTMDQueue, me->index); 20 | printf("Histogram index: %d TimeInterval: %d DataValue: %d\n", me->index, tmd.timeInterval, tmd.dataValue); 21 | me->index = TMDQueue_getNextIndex(me->itsTMDQueue, me->index); 22 | } 23 | 24 | /* operation updateHistogram() */ 25 | void HistogramDisplay_updateHistogram(HistogramDisplay* const me) { 26 | /* put some histogram stuff here… */ 27 | printf("call HistogramDisplay_updateHistogram\n"); 28 | } 29 | 30 | struct TMDQueue* HistogramDisplay_getItsTMDQueue(const HistogramDisplay* const me) { 31 | return (struct TMDQueue*)me->itsTMDQueue; 32 | } 33 | 34 | void HistogramDisplay_setItsTMDQueue(HistogramDisplay* const me, struct TMDQueue* p_TMDQueue) { 35 | me->itsTMDQueue = p_TMDQueue; 36 | } 37 | 38 | HistogramDisplay * HistogramDisplay_Create(void) { 39 | HistogramDisplay* me = (HistogramDisplay *) malloc(sizeof(HistogramDisplay)); 40 | if(me!=NULL) 41 | { 42 | HistogramDisplay_Init(me); 43 | } 44 | return me; 45 | } 46 | 47 | void HistogramDisplay_Destroy(HistogramDisplay* const me) { 48 | if(me!=NULL) 49 | { 50 | HistogramDisplay_Cleanup(me); 51 | } 52 | free(me); 53 | } 54 | 55 | static void cleanUpRelations(HistogramDisplay* const me) { 56 | if(me->itsTMDQueue != NULL) 57 | { 58 | me->itsTMDQueue = NULL; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /chapter1/02-Queue/CachedQueue.h: -------------------------------------------------------------------------------- 1 | #ifndef CACHEDQUEUE_H_ 2 | #define CACHEDQUEUE_H_ 3 | 4 | #include "Queue.h" 5 | 6 | typedef struct CachedQueue CachedQueue; 7 | struct CachedQueue 8 | { 9 | Queue* queue; //base class 10 | 11 | char filename[80]; 12 | 13 | int numberElementsOnDisk; 14 | Queue* outputQueue; 15 | 16 | // inherited vitual functions 17 | int (*isFull)(CachedQueue* const me); 18 | int (*isEmpty)(CachedQueue* const me); 19 | int (*getSize)(CachedQueue* const me); 20 | void (*insert)(CachedQueue* const me, int k); 21 | int (*remove)(CachedQueue* const me); 22 | //new virtual functions 23 | void (*flush)(CachedQueue* const me); 24 | void (*load)(CachedQueue* const me); 25 | }; 26 | 27 | // constructions and destructions 28 | void CachedQueue_Init(CachedQueue* const me, 29 | char* fName, 30 | int (*isFullFunc)(CachedQueue* const me), 31 | int (*isEmptyFunc)(CachedQueue* const me), 32 | int (*getSizeFunc)(CachedQueue* const me), 33 | void (*insertFunc)(CachedQueue* const me, int k), 34 | int (*removeFunc)(CachedQueue* const me), 35 | void (*flushFunc)(CachedQueue* const me), 36 | void (*loadFunc)(CachedQueue* const me) ); 37 | 38 | void CachedQueue_Cleanup(CachedQueue* const me); 39 | 40 | // operations 41 | int CachedQueue_isFull(CachedQueue* const me); 42 | int CachedQueue_isEmpty(CachedQueue* const me); 43 | int CachedQueue_getSize(CachedQueue* const me); 44 | void CachedQueue_insert(CachedQueue* const me, int k); 45 | int CachedQueue_remove(CachedQueue* const me); 46 | void CachedQueue_flush(CachedQueue* const me); 47 | void CachedQueue_load(CachedQueue* const me); 48 | 49 | CachedQueue* CachedQueue_Create(void); 50 | void CachedQueue_Destroy(CachedQueue* const me); 51 | 52 | #endif //CACHEDQUEUE_H_ 53 | -------------------------------------------------------------------------------- /chapter5/01-singleReceptor/TSRSyncSingleReceptor.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TokenizerSyncSingleReceptorPkg.h" 3 | 4 | struct Mutex; 5 | 6 | typedef struct TokenizerSyncSingleReceptor TokenizerSyncSingleReceptor; 7 | struct TokenizerSyncSingleReceptor { 8 | char ch; 9 | double result; 10 | TSTATETYPE stateID; 11 | TSUBSTATETYPE subStateID; 12 | double tensPlace; 13 | struct Mutex* itsMutex; 14 | }; 15 | 16 | void TokenizerSyncSingleReceptor_enter_GotNumber(TokenizerSyncSingleReceptor* const me); 17 | 18 | void TokenizerSyncSingleReceptor_enter_NoNumber(TokenizerSyncSingleReceptor* const me); 19 | 20 | void TokenizerSyncSingleReceptor_enter_ProcessingFractionalPart(TokenizerSyncSingleReceptor* const me); 21 | 22 | void TokenizerSyncSingleReceptor_enter_ProcessingWholePart(TokenizerSyncSingleReceptor* const me); 23 | 24 | void TokenizerSyncSingleReceptor_exit_GotNumber(TokenizerSyncSingleReceptor* const me); 25 | 26 | void TokenizerSyncSingleReceptor_exit_NoNumber(TokenizerSyncSingleReceptor* const me); 27 | 28 | void TokenizerSyncSingleReceptor_exit_ProcessingFractionalPart(TokenizerSyncSingleReceptor* const me); 29 | 30 | void TokenizerSyncSingleReceptor_exit_ProcessingWholePart(TokenizerSyncSingleReceptor* const me); 31 | 32 | void TokenizerSyncSingleReceptor_Init(TokenizerSyncSingleReceptor* const me); 33 | 34 | void TokenizerSyncSingleReceptor_Cleanup(TokenizerSyncSingleReceptor* const me); 35 | 36 | void TokenizerSyncSingleReceptor_eventDispatch(TokenizerSyncSingleReceptor* const me, Event e); 37 | 38 | struct Mutex* TokenizerSyncSingleReceptor_getItsMutex(const TokenizerSyncSingleReceptor* const me); 39 | 40 | void TokenizerSyncSingleReceptor_setItsMutex(TokenizerSyncSingleReceptor* const me, struct Mutex* p_Mutex); 41 | 42 | 43 | TokenizerSyncSingleReceptor * TokenizerSyncSingleReceptor_Create(void); 44 | 45 | 46 | void TokenizerSyncSingleReceptor_Destroy(TokenizerSyncSingleReceptor* const me); 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /chapter2/02-observer/TMDQueue.h: -------------------------------------------------------------------------------- 1 | #ifndef TMDQueue_H 2 | #define TMDQueue_H 3 | 4 | /*## auto_generated */ 5 | #include 6 | #include "ECGPkg.h" 7 | #include "TimeMarkedData.h" 8 | 9 | struct NotificationHandle; 10 | 11 | typedef struct TMDQueue TMDQueue; 12 | /* 13 | This queue is meant to operate as a "leaky" queue. In this queue, 14 | data is never removed per se, but is instead overwritten when the 15 | buffer pointer wraps around. This allows for many clients to read 16 | the same data from the queue. */ 17 | struct TMDQueue { 18 | int head; 19 | int nSubscribers; 20 | int size; 21 | struct TimeMarkedData buffer[QUEUE_SIZE]; 22 | struct NotificationHandle* itsNotificationHandle; 23 | }; 24 | 25 | void (*UpdateFuncPtr)(HistogramDisplay* const me, const struct TimeMarkedData tmd); 26 | 27 | /* Constructors and destructors:*/ 28 | void TMDQueue_Init(TMDQueue* const me); 29 | void TMDQueue_Cleanup(TMDQueue* const me); 30 | 31 | /* Operations */ 32 | int TMDQueue_getNextIndex(TMDQueue* const me, int index); 33 | void TMDQueue_insert(TMDQueue* const me, const struct TimeMarkedData tmd); 34 | boolean TMDQueue_isEmpty(TMDQueue* const me); 35 | void TMDQueue_notify(TMDQueue* const me, const struct TimeMarkedData tmd); 36 | struct TimeMarkedData TMDQueue_remove(TMDQueue* const me, int index); 37 | 38 | /* The NotificationHandle is managed as a linked list, with insertions coming at the end. */ 39 | void TMDQueue_subscribe(TMDQueue* const me, const UpdateFuncPtr updateFuncAddr); 40 | int TMDQueue_unsubscribe(TMDQueue* const me, const UpdateFuncPtr updateFuncAddr); 41 | 42 | int TMDQueue_getBuffer(const TMDQueue* const me); 43 | 44 | struct NotificationHandle* TMDQueue_getItsNotificationHandle(const TMDQueue* const me); 45 | void TMDQueue_setItsNotificationHandle(TMDQueue* const me, struct NotificationHandle* p_NotificationHandle); 46 | 47 | TMDQueue * TMDQueue_Create(void); 48 | void TMDQueue_Destroy(TMDQueue* const me); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /chapter4/06-ThreadBarrier/ThreadBarrier.c: -------------------------------------------------------------------------------- 1 | #include "ThreadBarrier.h" 2 | 3 | void ThreadBarrier_Init(ThreadBarrier* const me) { 4 | me->currentCount = 0; 5 | me->expectedCount = 3; 6 | me->mutex = OSSemaphore_Create(); 7 | me->barrier = OSSemaphore_Create(); 8 | if (me->barrier) { 9 | OSSemaphore_lock(me->barrier); 10 | printf("BARRIER IS LOCKED FIRST TIME\n"); 11 | }; 12 | } 13 | 14 | void ThreadBarrier_Cleanup(ThreadBarrier* const me) { 15 | OSSemaphore_Destroy(me->barrier); 16 | OSSemaphore_Destroy(me->mutex); 17 | } 18 | 19 | void ThreadBarrier_reset(ThreadBarrier* const me, int x) { 20 | me->expectedCount = x; 21 | me->currentCount = 0; 22 | } 23 | 24 | void ThreadBarrier_synchronize(ThreadBarrier* const me) { 25 | /* 26 | protect the critical region around 27 | the currentCount 28 | */ 29 | OSSemaphore_lock(me->mutex); 30 | ++me->currentCount; /* critical region */ 31 | OSSemaphore_release(me->mutex); 32 | 33 | /* 34 | are conditions for unblocking met? 35 | if so, then release the first blocked 36 | thread or the highest priority blocked 37 | thread (depending on the OS) 38 | */ 39 | if (me->currentCount == me->expectedCount) { 40 | printf("Conditions met\n"); 41 | OSSemaphore_release(me->barrier); 42 | }; 43 | 44 | /* 45 | lock the semaphore and when released 46 | then release it for the next blocked thread 47 | */ 48 | OSSemaphore_lock(me->barrier); 49 | OSSemaphore_release(me->barrier); 50 | 51 | } 52 | 53 | ThreadBarrier * ThreadBarrier_Create(void) { 54 | ThreadBarrier* me = (ThreadBarrier *) malloc(sizeof(ThreadBarrier)); 55 | if(me!=NULL) 56 | ThreadBarrier_Init(me); 57 | return me; 58 | } 59 | 60 | void ThreadBarrier_Destroy(ThreadBarrier* const me) { 61 | if(me!=NULL) 62 | ThreadBarrier_Cleanup(me); 63 | free(me); 64 | } 65 | -------------------------------------------------------------------------------- /chapter3/01-hardwareproxy/MotorProxy.h: -------------------------------------------------------------------------------- 1 | #ifndef MotorProxy_H 2 | #define MotorProxy_H 3 | 4 | #include "HWProxyExample.h" 5 | #include "MotorData.h" 6 | #include 7 | #include 8 | 9 | /* class MotorProxy */ 10 | typedef struct MotorProxy MotorProxy; 11 | /* This is the proxy for the motor hardware. */ 12 | /* Note that the speed of the motor is adjusted for the length of the rotary arm */ 13 | /* to keep a constant speed at the end of the arm. */ 14 | struct MotorProxy { 15 | unsigned int* motorData; 16 | unsigned int* motorAddr; 17 | unsigned int rotaryArmLength; 18 | }; 19 | 20 | void MotorProxy_Init(MotorProxy* const me); 21 | void MotorProxy_Cleanup(MotorProxy* const me); 22 | 23 | DirectionType MotorProxy_accessMotorDirection(MotorProxy* const me); 24 | unsigned int MotorProxy_accessMotorSpeed(MotorProxy* const me); 25 | unsigned int MotorProxy_aceessMotorState(MotorProxy* const me); 26 | 27 | /* keep all settings the same but clear error bits */ 28 | void MotorProxy_clearErrorStatus(MotorProxy* const me); 29 | 30 | /* Configure must be called first, since it sets up the */ 31 | /* address of the device. */ 32 | void MotorProxy_configure(MotorProxy* const me, unsigned int length, unsigned int* location, unsigned int* motorData); 33 | 34 | /* turn motor off but keep original settings */ 35 | void MotorProxy_disable(MotorProxy* const me); 36 | 37 | /* Start up the hardware but leave all other settings of the */ 38 | /* hardware alone */ 39 | void MotorProxy_enable(MotorProxy* const me); 40 | 41 | /* precondition: must be called AFTER configure() function. */ 42 | /* turn on the hardware to a known default state. */ 43 | void MotorProxy_initialize(MotorProxy* const me); 44 | 45 | /* update the speed and direction of the motor together */ 46 | void MotorProxy_writeMotorSpeed(MotorProxy* const me, const DirectionType direction, unsigned int speed); 47 | 48 | MotorProxy * MotorProxy_Create(void); 49 | void MotorProxy_Destroy(MotorProxy* const me); 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /chapter2/01-client-server/main.c: -------------------------------------------------------------------------------- 1 | #include "ECGPkg.h" 2 | #include "TestBuilder.h" 3 | 4 | int main(int argc, char* argv[]) { 5 | int status = 0; 6 | 7 | TestBuilder * p_TestBuilder; 8 | p_TestBuilder = TestBuilder_Create(); 9 | ECG_Module_acquireValue(&(p_TestBuilder->itsECG_Module)); 10 | ECG_Module_acquireValue(&(p_TestBuilder->itsECG_Module)); 11 | ECG_Module_acquireValue(&(p_TestBuilder->itsECG_Module)); 12 | ECG_Module_acquireValue(&(p_TestBuilder->itsECG_Module)); 13 | ECG_Module_acquireValue(&(p_TestBuilder->itsECG_Module)); 14 | 15 | HistogramDisplay_getValue(&(p_TestBuilder->itsHistogramDisplay)); 16 | HistogramDisplay_getValue(&(p_TestBuilder->itsHistogramDisplay)); 17 | HistogramDisplay_getValue(&(p_TestBuilder->itsHistogramDisplay)); 18 | HistogramDisplay_getValue(&(p_TestBuilder->itsHistogramDisplay)); 19 | HistogramDisplay_getValue(&(p_TestBuilder->itsHistogramDisplay)); 20 | 21 | QRSDetector_getDataSample(&(p_TestBuilder->itsQRSDetector)); 22 | QRSDetector_getDataSample(&(p_TestBuilder->itsQRSDetector)); 23 | QRSDetector_getDataSample(&(p_TestBuilder->itsQRSDetector)); 24 | 25 | WaveformDisplay_getScalarValue(&(p_TestBuilder->itsWaveformDisplay)); 26 | WaveformDisplay_getScalarValue(&(p_TestBuilder->itsWaveformDisplay)); 27 | WaveformDisplay_getScalarValue(&(p_TestBuilder->itsWaveformDisplay)); 28 | WaveformDisplay_getScalarValue(&(p_TestBuilder->itsWaveformDisplay)); 29 | WaveformDisplay_getScalarValue(&(p_TestBuilder->itsWaveformDisplay)); 30 | 31 | ArrhythmiaDetector_getDataSample(&(p_TestBuilder->itsArrhythmiaDetector)); 32 | ArrhythmiaDetector_getDataSample(&(p_TestBuilder->itsArrhythmiaDetector)); 33 | ArrhythmiaDetector_getDataSample(&(p_TestBuilder->itsArrhythmiaDetector)); 34 | ArrhythmiaDetector_getDataSample(&(p_TestBuilder->itsArrhythmiaDetector)); 35 | ArrhythmiaDetector_getDataSample(&(p_TestBuilder->itsArrhythmiaDetector)); 36 | 37 | printf("Done\n"); 38 | TestBuilder_Destroy(p_TestBuilder); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /chapter2/02-observer/main.c: -------------------------------------------------------------------------------- 1 | #include "ECGPkg.h" 2 | #include "TestBuilder.h" 3 | 4 | int main(int argc, char* argv[]) { 5 | int status = 0; 6 | 7 | TestBuilder * p_TestBuilder; 8 | p_TestBuilder = TestBuilder_Create(); 9 | ECG_Module_acquireValue(&(p_TestBuilder->itsECG_Module)); 10 | ECG_Module_acquireValue(&(p_TestBuilder->itsECG_Module)); 11 | ECG_Module_acquireValue(&(p_TestBuilder->itsECG_Module)); 12 | ECG_Module_acquireValue(&(p_TestBuilder->itsECG_Module)); 13 | ECG_Module_acquireValue(&(p_TestBuilder->itsECG_Module)); 14 | 15 | HistogramDisplay_getValue(&(p_TestBuilder->itsHistogramDisplay)); 16 | HistogramDisplay_getValue(&(p_TestBuilder->itsHistogramDisplay)); 17 | HistogramDisplay_getValue(&(p_TestBuilder->itsHistogramDisplay)); 18 | HistogramDisplay_getValue(&(p_TestBuilder->itsHistogramDisplay)); 19 | HistogramDisplay_getValue(&(p_TestBuilder->itsHistogramDisplay)); 20 | 21 | // QRSDetector_getDataSample(&(p_TestBuilder->itsQRSDetector)); 22 | // QRSDetector_getDataSample(&(p_TestBuilder->itsQRSDetector)); 23 | // QRSDetector_getDataSample(&(p_TestBuilder->itsQRSDetector)); 24 | 25 | // WaveformDisplay_getScalarValue(&(p_TestBuilder->itsWaveformDisplay)); 26 | // WaveformDisplay_getScalarValue(&(p_TestBuilder->itsWaveformDisplay)); 27 | // WaveformDisplay_getScalarValue(&(p_TestBuilder->itsWaveformDisplay)); 28 | // WaveformDisplay_getScalarValue(&(p_TestBuilder->itsWaveformDisplay)); 29 | // WaveformDisplay_getScalarValue(&(p_TestBuilder->itsWaveformDisplay)); 30 | 31 | // ArrhythmiaDetector_getDataSample(&(p_TestBuilder->itsArrhythmiaDetector)); 32 | // ArrhythmiaDetector_getDataSample(&(p_TestBuilder->itsArrhythmiaDetector)); 33 | // ArrhythmiaDetector_getDataSample(&(p_TestBuilder->itsArrhythmiaDetector)); 34 | // ArrhythmiaDetector_getDataSample(&(p_TestBuilder->itsArrhythmiaDetector)); 35 | // ArrhythmiaDetector_getDataSample(&(p_TestBuilder->itsArrhythmiaDetector)); 36 | 37 | printf("Done\n"); 38 | TestBuilder_Destroy(p_TestBuilder); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /chapter5/03-StateTable/TokenizerStateTable.h: -------------------------------------------------------------------------------- 1 | #ifndef TokenizerStateTable_H 2 | #define TokenizerStateTable_H 3 | 4 | #include "StateTablePattern.h" 5 | struct Mutex; 6 | 7 | typedef struct TokenizerStateTable TokenizerStateTable; 8 | struct TokenizerStateTable { 9 | char ch; 10 | double result; 11 | TSTATETYPE stateID; 12 | TableEntryType table[GN_PROCESSINGFRACTIONALPART_STATE+1][EVENDOFSTRING+1]; 13 | double tensPlace; 14 | struct Mutex* itsMutex; 15 | }; 16 | 17 | /* Constructors and destructors:*/ 18 | void TokenizerStateTable_Init(TokenizerStateTable* const me); 19 | 20 | void TokenizerStateTable_Cleanup(TokenizerStateTable* const me); 21 | 22 | /* Operations */ 23 | void TokenizerStateTable_eventDispatch(TokenizerStateTable* const me, Event e); 24 | 25 | void TokenizerStateTable_enter_GotNumber(TokenizerStateTable* const me); 26 | 27 | void TokenizerStateTable_enter_NoNumber(TokenizerStateTable* const me); 28 | 29 | void TokenizerStateTable_enter_ProcessingFractionalPart(TokenizerStateTable* const me); 30 | 31 | void TokenizerStateTable_enter_ProcessingWholePart(TokenizerStateTable* const me); 32 | 33 | void TokenizerStateTable_exit_GotNumber(TokenizerStateTable* const me); 34 | 35 | void TokenizerStateTable_exit_NoNumber(TokenizerStateTable* const me); 36 | 37 | void TokenizerStateTable_exit_ProcessingFractionalPart(TokenizerStateTable* const me); 38 | 39 | void TokenizerStateTable_exit_ProcessingWholePart(TokenizerStateTable* const me); 40 | 41 | struct Mutex* TokenizerStateTable_getItsMutex(const TokenizerStateTable* const me); 42 | 43 | void TokenizerStateTable_setItsMutex(TokenizerStateTable* const me, struct Mutex* p_Mutex); 44 | 45 | TokenizerStateTable * TokenizerStateTable_Create(void); 46 | 47 | void TokenizerStateTable_Destroy(TokenizerStateTable* const me); 48 | 49 | void TokenizerStateTable_assignCh(TokenizerStateTable* const me, char c); 50 | 51 | void TokenizerStateTable_NoNum2GN(TokenizerStateTable* const me, char c); 52 | 53 | void TokenizerStateTable_Frac2Frac(TokenizerStateTable* const me, char c); 54 | 55 | void TokenizerStateTable_printResult(TokenizerStateTable* const me); 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /chapter6/02-crc/PatientData.h: -------------------------------------------------------------------------------- 1 | #ifndef PatientData_H 2 | #define PatientData_H 3 | 4 | #include "CRCExample.h" 5 | struct AlarmManager; 6 | 7 | typedef struct PatientData PatientData; 8 | struct PatientData { 9 | PatientDataType pData; 10 | unsigned short crc; 11 | struct AlarmManager* itsAlarmManager; 12 | }; 13 | 14 | void PatientData_Init(PatientData* const me); 15 | void PatientData_Cleanup(PatientData* const me); 16 | 17 | /* Operations */ 18 | void PatientData_errorHandler(PatientData* const me, ErrorCodeType errCode); 19 | 20 | unsigned short PatientData_getAge(PatientData* const me); 21 | 22 | unsigned short PatientData_getBloodO2Conc(PatientData* const me); 23 | 24 | unsigned short PatientData_getDiastolicBP(PatientData* const me); 25 | 26 | GenderType PatientData_getGender(PatientData* const me); 27 | 28 | char* PatientData_getName(PatientData* const me); 29 | 30 | unsigned long PatientData_getPatientID(PatientData* const me); 31 | 32 | unsigned short PatientData_getSystolicBP(PatientData* const me); 33 | 34 | unsigned short PatientData_getTemperature(PatientData* const me); 35 | 36 | double PatientData_getWeight(PatientData* const me); 37 | 38 | void PatientData_setAge(PatientData* const me, unsigned short a); 39 | 40 | void PatientData_setBloodO2Conc(PatientData* const me, unsigned short o2); 41 | 42 | void PatientData_setDiastolicBP(PatientData* const me, unsigned short dBP); 43 | 44 | void PatientData_setGender(PatientData* const me, GenderType g); 45 | 46 | void PatientData_setName(PatientData* const me, char* n); 47 | 48 | void PatientData_setPatientID(PatientData* const me, unsigned long id); 49 | 50 | void PatientData_setSystolicBP(PatientData* const me, unsigned short sBP); 51 | 52 | void PatientData_setTemperature(PatientData* const me, unsigned short t); 53 | 54 | void PatientData_setWeight(PatientData* const me, double w); 55 | 56 | int PatientData_checkData(PatientData* const me); 57 | 58 | struct AlarmManager* PatientData_getItsAlarmManager(const PatientData* const me); 59 | 60 | void PatientData_setItsAlarmManager(PatientData* const me, struct AlarmManager* p_AlarmManager); 61 | 62 | PatientData * PatientData_Create(void); 63 | 64 | void PatientData_Destroy(PatientData* const me); 65 | #endif 66 | -------------------------------------------------------------------------------- /chapter6/05-protectsinglechannel/Thermostat.c: -------------------------------------------------------------------------------- 1 | #include "Thermostat.h" 2 | #include "FurnaceController.h" 3 | 4 | static void cleanUpRelations(Thermostat* const me); 5 | 6 | void Thermostat_Init(Thermostat* const me) { 7 | me->defaultTempSetting = 70; 8 | me->invertedDefaultTemp = ~70; 9 | me->itsFurnaceController = NULL; 10 | } 11 | 12 | void Thermostat_Cleanup(Thermostat* const me) { 13 | cleanUpRelations(me); 14 | } 15 | 16 | int Thermostat_getDesiredTemperature(Thermostat* const me) { 17 | if (me->desiredTemp == ~me->invertedDesiredTemp) 18 | return me->desiredTemp; 19 | else 20 | if (me->defaultTempSetting == ~me->invertedDefaultTemp) { 21 | Thermostat_setDesiredTemperature(me, me->defaultTempSetting); 22 | return me->defaultTempSetting; 23 | } 24 | else { 25 | FurnaceController_shutDown(me->itsFurnaceController); 26 | return me->defaultTempSetting; 27 | }; 28 | } 29 | 30 | void Thermostat_setDesiredTemperature(Thermostat* const me, int temp) { 31 | if (me->desiredTemp == ~me->invertedDesiredTemp) { 32 | me->desiredTemp = temp; 33 | me->invertedDesiredTemp = ~temp; 34 | if (me->desiredTemp == ~me->invertedDesiredTemp) 35 | return; 36 | } 37 | else 38 | FurnaceController_shutDown(me->itsFurnaceController); 39 | } 40 | 41 | struct FurnaceController* Thermostat_getItsFurnaceController(const Thermostat* const me) { 42 | return (struct FurnaceController*)me->itsFurnaceController; 43 | } 44 | 45 | void Thermostat_setItsFurnaceController(Thermostat* const me, struct FurnaceController* p_FurnaceController) { 46 | me->itsFurnaceController = p_FurnaceController; 47 | } 48 | 49 | Thermostat * Thermostat_Create(void) { 50 | Thermostat* me = (Thermostat *) malloc(sizeof(Thermostat)); 51 | if(me!=NULL) 52 | Thermostat_Init(me); 53 | return me; 54 | } 55 | 56 | void Thermostat_Destroy(Thermostat* const me) { 57 | if(me!=NULL) 58 | Thermostat_Cleanup(me); 59 | free(me); 60 | } 61 | 62 | static void cleanUpRelations(Thermostat* const me) { 63 | if(me->itsFurnaceController != NULL) 64 | me->itsFurnaceController = NULL; 65 | } 66 | -------------------------------------------------------------------------------- /chapter6/05-protectsinglechannel/Thermometer.c: -------------------------------------------------------------------------------- 1 | #include "Thermometer.h" 2 | #include "CheckTemperature.h" 3 | #include "FurnaceController.h" 4 | 5 | static void cleanUpRelations(Thermometer* const me); 6 | 7 | void Thermometer_Init(Thermometer* const me) { 8 | me->itsCheckTemperature = NULL; 9 | me->itsFurnaceController = NULL; 10 | } 11 | 12 | void Thermometer_Cleanup(Thermometer* const me) { 13 | cleanUpRelations(me); 14 | } 15 | 16 | void Thermometer_acquireMeasuredTemperature(Thermometer* const me) { 17 | /* access the hardware here */ 18 | } 19 | 20 | int Thermometer_getMeasuredTemperature(Thermometer* const me) { 21 | if (me->measuredTemp == ~me->invertedMeasuredTemp) { 22 | CheckTemperature_checkTemperature(me->itsCheckTemperature, me->measuredTemp); 23 | return me->measuredTemp; 24 | } 25 | else { 26 | FurnaceController_shutDown(me->itsFurnaceController); 27 | return 0; 28 | } 29 | } 30 | 31 | struct CheckTemperature* Thermometer_getItsCheckTemperature(const Thermometer* const me) { 32 | return (struct CheckTemperature*)me->itsCheckTemperature; 33 | } 34 | 35 | void Thermometer_setItsCheckTemperature(Thermometer* const me, struct CheckTemperature* p_CheckTemperature) { 36 | me->itsCheckTemperature = p_CheckTemperature; 37 | } 38 | 39 | struct FurnaceController* Thermometer_getItsFurnaceController(const Thermometer* const me) { 40 | return (struct FurnaceController*)me->itsFurnaceController; 41 | } 42 | 43 | void Thermometer_setItsFurnaceController(Thermometer* const me, struct FurnaceController* p_FurnaceController) { 44 | me->itsFurnaceController = p_FurnaceController; 45 | } 46 | 47 | Thermometer * Thermometer_Create(void) { 48 | Thermometer* me = (Thermometer *) malloc(sizeof(Thermometer)); 49 | if(me!=NULL) 50 | Thermometer_Init(me); 51 | return me; 52 | } 53 | 54 | void Thermometer_Destroy(Thermometer* const me) { 55 | if(me!=NULL) 56 | Thermometer_Cleanup(me); 57 | free(me); 58 | } 59 | 60 | static void cleanUpRelations(Thermometer* const me) { 61 | if(me->itsCheckTemperature != NULL) 62 | me->itsCheckTemperature = NULL; 63 | if(me->itsFurnaceController != NULL) 64 | me->itsFurnaceController = NULL; 65 | } 66 | -------------------------------------------------------------------------------- /chapter1/02-Queue/Queue.c: -------------------------------------------------------------------------------- 1 | #include "Queue.h" 2 | #include 3 | #include 4 | 5 | void Queue_Init(Queue* const me, 6 | int (*isFullFunc)(Queue* const me), 7 | int (*isEmptyFunc)(Queue* const me), 8 | int (*getSizeFunc)(Queue* const me), 9 | int (*insertFunc)(Queue* const me, int k), 10 | int (*removeFunc)(Queue* const me)) { 11 | // initialize attributes 12 | me->head = 0; 13 | me->size = 0; 14 | me->tail = 0; 15 | // initialize member function pointers 16 | me->isFull = isFullFunc; 17 | me->isEmpty = isEmptyFunc; 18 | me->getSize = getSizeFunc; 19 | me->insert = insertFunc; 20 | me->remove = removeFunc; 21 | } 22 | 23 | // operation cleanup 24 | void Queue_Cleanup(Queue* const me) { 25 | printf("call Queue_Cleanup\n"); 26 | printf("nothong to clean.\n"); 27 | } 28 | 29 | // operation isFull 30 | int Queue_isFull(Queue* const me) { 31 | return (me->head+1)%QUEUE_SIZE == me->tail; 32 | } 33 | 34 | // operation isEmpty 35 | int Queue_isEmpty(Queue* const me) { 36 | return (me->head == me->tail); 37 | } 38 | 39 | // operation getSize 40 | int Queue_getSize(Queue* const me) { 41 | return me->size; 42 | } 43 | 44 | // operation insert 45 | int Queue_insert(Queue* const me, int k) { 46 | if (!me->isFull(me)) 47 | { 48 | /* code */ 49 | me->buffer[me->head] = k; 50 | me->head = (me->head+1)%QUEUE_SIZE; 51 | ++me->size; 52 | } 53 | } 54 | 55 | // operation remove 56 | int Queue_remove(Queue* const me) { 57 | int value = -9999; 58 | if(!me->isEmpty(me)) { 59 | value = me->buffer[me->tail]; 60 | me->tail = (me->tail+1)%QUEUE_SIZE; 61 | --me->size; 62 | } 63 | return value; 64 | } 65 | 66 | // operation create 67 | Queue* Queue_Create(void) { 68 | Queue* me = (Queue*)malloc(sizeof(Queue)); 69 | if(me!=NULL){ 70 | Queue_Init(me, Queue_isFull, Queue_isEmpty, Queue_getSize, Queue_insert, Queue_remove); 71 | } 72 | return me; 73 | } 74 | 75 | // operation destroy 76 | void Queue_Destroy(Queue* const me) { 77 | if (me!=NULL) 78 | { 79 | /* code */ 80 | Queue_Cleanup(me); 81 | } 82 | free(me); 83 | } 84 | -------------------------------------------------------------------------------- /chapter4/08-orderLock/ResourceList.c: -------------------------------------------------------------------------------- 1 | #include "ResourceList.h" 2 | #include "Mutex.h" 3 | 4 | static char isInOrder(ResourceList* const me, int rID); 5 | 6 | static void cleanUpRelations(ResourceList* const me); 7 | 8 | void ResourceList_Init(ResourceList* const me) { 9 | me->nIDs = 0; 10 | me->itsMutex = NULL; 11 | } 12 | 13 | void ResourceList_Cleanup(ResourceList* const me) { 14 | cleanUpRelations(me); 15 | } 16 | 17 | int ResourceList_addLock(ResourceList* const me, int rID) { 18 | int retVal; 19 | Mutex_lock(me->itsMutex); 20 | if (isInOrder(me,rID)) { 21 | me->rIDList[me->nIDs++] = rID; 22 | retVal = 0; 23 | } 24 | else 25 | if (rID == me->rIDList[me->nIDs]) 26 | retVal = DUPLICATED_IDS; 27 | else 28 | retVal = POORLY_ORDERED_ACCESS; 29 | Mutex_release(me->itsMutex); 30 | return retVal; 31 | } 32 | 33 | void ResourceList_removeLock(ResourceList* const me, int rID) { 34 | int j,k; 35 | 36 | if (me->nIDs) { 37 | for (j=0; jnIDs; j++) { 38 | if (rID == me->rIDList[j]) { 39 | for (k=j; knIDs-1; k++) 40 | me->rIDList[k] = me->rIDList[k+1]; 41 | --me->nIDs; 42 | break; 43 | }; 44 | }; 45 | }; 46 | } 47 | 48 | static char isInOrder(ResourceList* const me, int rID) { 49 | if (me->nIDs) 50 | return rID > me->rIDList[me->nIDs-1]; 51 | else 52 | return 1; 53 | } 54 | 55 | struct Mutex* ResourceList_getItsMutex(const ResourceList* const me) { 56 | return (struct Mutex*)me->itsMutex; 57 | } 58 | 59 | void ResourceList_setItsMutex(ResourceList* const me, struct Mutex* p_Mutex) { 60 | me->itsMutex = p_Mutex; 61 | } 62 | 63 | ResourceList * ResourceList_Create(void) { 64 | ResourceList* me = (ResourceList *) malloc(sizeof(ResourceList)); 65 | if(me!=NULL) 66 | ResourceList_Init(me); 67 | return me; 68 | } 69 | 70 | void ResourceList_Destroy(ResourceList* const me) { 71 | if(me!=NULL) 72 | ResourceList_Cleanup(me); 73 | free(me); 74 | } 75 | 76 | static void cleanUpRelations(ResourceList* const me) { 77 | if(me->itsMutex != NULL) 78 | me->itsMutex = NULL; 79 | } 80 | -------------------------------------------------------------------------------- /chapter6/01-OwnShipAttitude/OwnShipAttitude.c: -------------------------------------------------------------------------------- 1 | #include "OwnShipAttitude.h" 2 | #include "AlarmManager.h" 3 | 4 | static void cleanUpRelations(OwnShipAttitude* const me); 5 | 6 | void OwnShipAttitude_Init(OwnShipAttitude* const me) { 7 | AttitudeDataType_Init(&(me->attitude)); 8 | AttitudeDataType_Init(&(me->invertedAttitude)); 9 | me->itsAlarmManager = NULL; 10 | } 11 | 12 | void OwnShipAttitude_Cleanup(OwnShipAttitude* const me) { 13 | cleanUpRelations(me); 14 | } 15 | 16 | void OwnShipAttitude_errorHandler(OwnShipAttitude* const me) { 17 | AlarmManager_addAlarm(me->itsAlarmManager, ATTITUDE_MEMORY_FAULT); 18 | } 19 | 20 | int OwnShipAttitude_getAttitude(OwnShipAttitude* const me, AttitudeDataType * aPtr) { 21 | AttitudeDataType ia = OwnShipAttitude_invert(me, me->invertedAttitude); 22 | 23 | if (me->attitude.roll == ia.roll && me->attitude.yaw == ia.yaw && 24 | me->attitude.pitch == ia.pitch ) { 25 | *aPtr = me->attitude; 26 | return 1; 27 | } 28 | else { 29 | OwnShipAttitude_errorHandler(me); 30 | return 0; 31 | }; 32 | } 33 | 34 | AttitudeDataType OwnShipAttitude_invert(OwnShipAttitude* const me, AttitudeDataType a) { 35 | a.roll = ~a.roll; 36 | a.yaw = ~a.yaw; 37 | a.pitch = ~a.pitch; 38 | return a; 39 | } 40 | 41 | void OwnShipAttitude_setAttitude(OwnShipAttitude* const me, AttitudeDataType a) { 42 | me->attitude = a; 43 | me->invertedAttitude = OwnShipAttitude_invert(me, a); 44 | } 45 | 46 | struct AlarmManager* OwnShipAttitude_getItsAlarmManager(const OwnShipAttitude* const me) { 47 | return (struct AlarmManager*)me->itsAlarmManager; 48 | } 49 | 50 | void OwnShipAttitude_setItsAlarmManager(OwnShipAttitude* const me, struct AlarmManager* p_AlarmManager) { 51 | me->itsAlarmManager = p_AlarmManager; 52 | } 53 | 54 | OwnShipAttitude * OwnShipAttitude_Create(void) { 55 | OwnShipAttitude* me = (OwnShipAttitude *) malloc(sizeof(OwnShipAttitude)); 56 | if(me!=NULL) 57 | OwnShipAttitude_Init(me); 58 | return me; 59 | } 60 | 61 | void OwnShipAttitude_Destroy(OwnShipAttitude* const me) { 62 | if(me!=NULL) 63 | OwnShipAttitude_Cleanup(me); 64 | free(me); 65 | } 66 | 67 | static void cleanUpRelations(OwnShipAttitude* const me) { 68 | if(me->itsAlarmManager != NULL) 69 | me->itsAlarmManager = NULL; 70 | } 71 | -------------------------------------------------------------------------------- /chapter3/07-poll/BCPeriodicPoller.h: -------------------------------------------------------------------------------- 1 | #ifndef BCPeriodicPoller_H 2 | #define BCPeriodicPoller_H 3 | 4 | #include 5 | #include 6 | 7 | typedef int deviceData; 8 | typedef void (*timerVectorPtr)(void); 9 | #define MAX_POLL_DEVICES (10) 10 | #define DEFAULT_POLL_TIME (1000) 11 | 12 | struct BCTimer; 13 | struct BreathingCircuitSensor; 14 | struct MedicalDisplay; 15 | 16 | typedef struct BCPeriodicPoller BCPeriodicPoller; 17 | struct BCPeriodicPoller { 18 | unsigned long pollTime; 19 | struct BCTimer* itsBCTimer; 20 | struct BreathingCircuitSensor *itsBreathingCircuitSensor[3]; 21 | struct MedicalDisplay* itsMedicalDisplay; 22 | }; 23 | 24 | 25 | void BCPeriodicPoller_Init(BCPeriodicPoller* const me); 26 | void BCPeriodicPoller_Cleanup(BCPeriodicPoller* const me); 27 | 28 | /* Operations */ 29 | void BCPeriodicPoller_poll(BCPeriodicPoller* const me); 30 | void BCPeriodicPoller_setPollTime(BCPeriodicPoller* const me, unsigned long t); 31 | void BCPeriodicPoller_startPolling(BCPeriodicPoller* const me); 32 | void BCPeriodicPoller_stopPolling(BCPeriodicPoller* const me); 33 | 34 | struct BCTimer* BCPeriodicPoller_getItsBCTimer(const BCPeriodicPoller* const me); 35 | void BCPeriodicPoller_setItsBCTimer(BCPeriodicPoller* const me, struct BCTimer* p_BCTimer); 36 | 37 | int BCPeriodicPoller_getItsBreathingCircuitSensor(const BCPeriodicPoller* const me); 38 | void BCPeriodicPoller_addItsBreathingCircuitSensor(BCPeriodicPoller* const me, struct BreathingCircuitSensor * p_BreathingCircuitSensor); 39 | 40 | void BCPeriodicPoller_removeItsBreathingCircuitSensor(BCPeriodicPoller* const me, struct BreathingCircuitSensor * p_BreathingCircuitSensor); 41 | void BCPeriodicPoller_clearItsBreathingCircuitSensor(BCPeriodicPoller* const me); 42 | 43 | struct MedicalDisplay* BCPeriodicPoller_getItsMedicalDisplay(const BCPeriodicPoller* const me); 44 | void BCPeriodicPoller_setItsMedicalDisplay(BCPeriodicPoller* const me, struct MedicalDisplay* p_MedicalDisplay); 45 | 46 | BCPeriodicPoller * BCPeriodicPoller_Create(void); 47 | void BCPeriodicPoller_Destroy(BCPeriodicPoller* const me); 48 | 49 | void BCPeriodicPoller_setItsBCTimer2(BCPeriodicPoller* const me, struct BCTimer* p_BCTimer); 50 | void BCPeriodicPoller_setItsBCTimer1(BCPeriodicPoller* const me, struct BCTimer* p_BCTimer); 51 | void BCPeriodicPoller_clearItsBCTimer(BCPeriodicPoller* const me); 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /chapter1/01-Sensor/Sensor.c: -------------------------------------------------------------------------------- 1 | #include "Sensor.h" 2 | #include 3 | #include 4 | #include 5 | 6 | void Sensor_Init(Sensor* const me) { 7 | me->filterFrequency = 0; 8 | me->updateFrequency = 0; 9 | me->value = 0; 10 | } 11 | 12 | void Sensor_Cleanup(Sensor* const me) { 13 | printf("call Sensor_Cleanup\n"); 14 | printf("nothong to cleanup\n"); 15 | } 16 | 17 | //========================================================================== 18 | 19 | int Sensor_getFilterFrequency(const Sensor* const me) { 20 | return me->filterFrequency; 21 | } 22 | 23 | void Sensor_setFilterFrequency(Sensor* const me, int p_filterFrequency) { 24 | me->filterFrequency = p_filterFrequency; 25 | } 26 | 27 | int Sensor_getUpdateFrequency(const Sensor* const me) { 28 | return me->updateFrequency; 29 | } 30 | 31 | void Sensor_setUpdateFrequency(Sensor* const me, int p_updateFrequency) { 32 | me->updateFrequency = p_updateFrequency; 33 | } 34 | 35 | int Sensor_getValue(const Sensor* const me) { 36 | return me->value; 37 | } 38 | 39 | int Sensor_acquireValue(Sensor* me) { 40 | int *r, *w; // read and write addressed 41 | int j; 42 | 43 | switch(me->whatKindOfInterface) { 44 | case MEMORY_MAPPED: 45 | w = (int*)WRITE_ADDR; // address to write to sensor 46 | *w = WRITE_MASK; //sensor command to force a read 47 | for (int i = 0; i < 100; ++i) 48 | { 49 | /* wait loop */ 50 | } 51 | r = (int*)READ_ADDR; // address of returned value 52 | me->value = *r; 53 | break; 54 | case PORT_MAPPED: 55 | // me->value = inp(SENSOR_PORT); // inp() is a compiler-specific port function 56 | me->value = (int)SENSOR_PORT; 57 | break; 58 | } // end switch 59 | return me->value; 60 | } 61 | 62 | //========================================================================== 63 | Sensor* Sensor_Create(void) { 64 | Sensor* me = (Sensor*)malloc(sizeof(Sensor)); 65 | if (me != NULL) 66 | { 67 | /* code */ 68 | Sensor_Init(me); 69 | } 70 | return me; 71 | } 72 | 73 | void Sensor_Destroy(Sensor* const me) { 74 | if (me != NULL) 75 | { 76 | /* code */ 77 | Sensor_Cleanup(me); 78 | } 79 | free(me); 80 | } 81 | --------------------------------------------------------------------------------