Se ha denunciado esta presentación.
Se está descargando tu SlideShare. ×

DesignPatterns-IntroPresentation.pptx

Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Próximo SlideShare
from java to c
from java to c
Cargando en…3
×

Eche un vistazo a continuación

1 de 62 Anuncio

Más Contenido Relacionado

Similares a DesignPatterns-IntroPresentation.pptx (20)

Más reciente (20)

Anuncio

DesignPatterns-IntroPresentation.pptx

  1. 1. Design Patterns for Embedded Systems
  2. 2. Design Patterns State Management Behavior Resource Management API Design Reusability Interaction Management Complexity Distribution Data Organization
  3. 3. Special members ctor dtor Object: Object Data Object Methods Data Methods other special methods Implementation data - id - size - debug info
  4. 4. Implementation example for a string-like object:
  5. 5. object ptr class ptr size ctor dtor clone other special members object data object class object
  6. 6. typedef struct { size_t size; void* (*ctor) (void* self, va_list* app); void* (*dtor) (void* self); void* (*clone) (const void* self); } Class; Interface: Class.h size ctor dtor clone class object
  7. 7. void* new(const void* const _class, ...); void* clone(const void* const self); void delete(void* self); Interface: Class.h
  8. 8. Interface: Class.h __attribute__((warn_unused_result)) void* new(const void* const _class, ...); __attribute__((warn_unused_result)) void* clone(const void* const self); void delete(void* self);
  9. 9. void* new(const void* const _class, ...) { const Class* const class = _class; void* instance = malloc(class->size); assert(instance); * (Class**) instance = (Class*) class; if(class->ctor) { va_list ap; va_start(ap, _class); instance = class->ctor(instance, &ap); va_end(ap); } return instance; } Implementation: Class.c
  10. 10. TypeClass ptr size Instance ptr malloc assign ctor dtor clone data ptr TypeClass ptr allocates returns new parameter
  11. 11. object ptr size ctor dtor clone TypeClass ptr Instance ptr data ctor TypeClass ptr Instance ptr data initialize return
  12. 12. void delete(void* self) { const Class** class_ptr = self; if(self && *class_ptr && (*class_ptr)->dtor) { self = (*class_ptr)->dtor(self); } free(self); } Implementation: Class.c
  13. 13. data data size ctor dtor clone TypeClass ptr Object ptr delete parameter Is NULL? Does it have special members? Does it have a destructor? dtor free return
  14. 14. void* clone(const void* self) { const Class** class_ptr = (const Class**) self; if(self && *class_ptr && (*class_ptr)->clone) { return (*class_ptr)->clone(self); } return NULL; } Implementation: Class.c
  15. 15. data size ctor dtor clone TypeClass ptr Object ptr clone parameter Is NULL? Does it have special members? Does it have a clone? clone return
  16. 16. typedef struct { const void* _class; char* text; } String; extern const String* const StringClass; Interface: String.h class ptr object data object
  17. 17. static void* String_ctor( void* const _self, va_list* app ) { String* const self = _self; const char* text = va_arg(*app, const char*); self->text = malloc(strlen(text) + 1); assert(self->text); strcpy(self->text, text); return self; } Implementation: String.c
  18. 18. static void* String_dtor(void* const _self) { String* const self = _self; free(self->text); self->text = NULL; return self; } Implementation: String.c
  19. 19. static void* String_clone(const void* const _self) { const String* const self = _self; return new(String, self->text); } Implementation: String.c
  20. 20. static const Class StringSpecialMembers = { sizeof(String), String_ctor, String_dtor, String_clone }; const String* const StringClass = (String*) &StringSpecialMembers; Implementation: String.c
  21. 21. String* a = new(StringClass, “object an"); String* b = new(StringClass, “object bn"); String* copy_of_b = clone(b); printf(a->text); printf(b->text); printf(copy_of_b->text); delete(copy_of_b); delete(a); delete(b); Example:
  22. 22. Interface: Class.h __attribute__((warn_unused_result)) void* new(const void* const _class, ...); __attribute__((warn_unused_result)) void* clone(const void* const self); void delete(void* self);
  23. 23. Interface: Class.h #define new(type, ...) _new(type##Class, __VA_ARGS__) __attribute__((warn_unused_result)) void* _new(const void* const _class, ...); __attribute__((warn_unused_result)) void* clone(const void* const self); void delete(void* self);
  24. 24. String* a = new(String, “object an"); String* b = new(String, “object bn"); String* copy_of_b = clone(b); printf(a->text); printf(b->text); printf(copy_of_b->text); delete(copy_of_b); delete(a); delete(b); Example:
  25. 25. >>>>>> Example:
  26. 26. Member_1 Member_1 More_stuff Object Object with more stuff Member_2 Member_2
  27. 27. logData format PublicLogger PrivateLogger changeFormat logData changeFormat logData Logger changeFormat Implementation Interface
  28. 28. Singleton - Interface typedef struct { bool (*const logData) (const int data_id, const int data); void (*const changeFormat) (const char* const format); } Logger; Logger* getLogger(void); logData Logger changeFormat
  29. 29. Singleton - Implementation typedef Logger PublicLogger; typedef struct { PublicLogger pub; const char* format; } PrivateLogger; format PrivateLogger logData changeFormat logData PublicLogger changeFormat
  30. 30. Singleton - Implementation static bool logData(int const data_id, int const data) { const PrivateLogger* const logger = (PrivateLogger*) getLogger(); printf(logger->format, data_id, data); /* or something like YourFavoriteProtocol_Send(); */ return true; }
  31. 31. Singleton static void changeFormat (const char* const format) { PrivateLogger* logger = (PrivateLogger*) getLogger(); logger->format = format; }
  32. 32. Logger* getLogger(void) { static PrivateLogger logger = { /* initialise public members : PublicLogger*/ .pub = { logData, changeFormat }, /* initialise private members */ .format = "data_%d = %dn", }; return ((PublicLogger*) &logger); }
  33. 33. Singleton Logger* log1 = getLogger(); Logger* log2 = getLogger(); log1->logData(1, 2); log2->logData(23, 535); log1->changeFormat("logging data with ID: #%d -> value = %dn"); log1->logData(1, 2); log2->logData(23, 535);
  34. 34. >>>>>> Example:
  35. 35. Hardware Proxy deviceAddress: - Address to memory mapped device or device port number or some other location identifier - Type - void pointer - struct matching the memory mapped registers
  36. 36. typedef struct { /*!< GPIO Structure */ __IO uint32_t DATA[256]; /*!< GPIO Data */ __IO uint32_t DIR; /*!< GPIO Direction */ __IO uint32_t IS; /*!< GPIO Interrupt Sense */ __IO uint32_t IBE; /*!< GPIO Interrupt Both Edges */ __IO uint32_t IEV; /*!< GPIO Interrupt Event */ __IO uint32_t IM; /*!< GPIO Interrupt Mask */ __IO uint32_t RIS; /*!< GPIO Raw Interrupt Status */ __IO uint32_t MIS; /*!< GPIO Masked Interrupt Status */ __O uint32_t ICR; /*!< GPIO Interrupt Clear */ __IO uint32_t AFSEL; /*!< GPIO Alternate Function Select */ __I uint32_t RESERVED1[55]; __IO uint32_t DR2R; /*!< GPIO 2-mA Drive Select */ . . . } GPIO_Type;
  37. 37. #define GPIOA ((GPIO_Type*) GPIO_PORTA_BASE) #define GPIOB ((GPIO_Type*) GPIO_PORTB_BASE) #define GPIOC ((GPIO_Type*) GPIO_PORTC_BASE) #define GPIOD ((GPIO_Type*) GPIO_PORTD_BASE)
  38. 38. Hardware Proxy Initialize(): - Initialize device prior to first use - Might be empty - Might take parameters
  39. 39. Hardware Proxy Configure(): - Configuration data as: - Individual parameters - Custom configuration type
  40. 40. Hardware Proxy disable(): - Safely turn off device - Might be empty - May or may not have parameters
  41. 41. Hardware Proxy access(): - Read data from device - May be implemented as a set of getters
  42. 42. Hardware Proxy mutate(): - Writes data to the device - May be implemented as a set of setters
  43. 43. Hardware Proxy marshal(): - Used to hide the peculiarities of the device interface - Performs any required - encryption - compression - Bit-packing
  44. 44. Hardware Proxy unmarshal(): - Used before accessing data - Performs any necessary - unpacking - decryption - decompression
  45. 45. Hardware Adapter
  46. 46. Hardware Adapter
  47. 47. Hardware Mediator
  48. 48. Façade
  49. 49. Façade
  50. 50. Façade + Mediator
  51. 51. Opportunistic Polling Pattern while(1) { poll(); } eventActivation() { poll(); } void poll(void) { /* Update value in buffer */ SPI_Device.UpdateData(TemperatureSensor); /* Update display value */ UpdateDisplay.manageData(TemperatureSensor); UpdateDisplay.manageData(PressureSensor); /* Check device status */ ActivatePeriodicCheck(); }
  52. 52. Periodic Polling Pattern
  53. 53. Chanel Pattern
  54. 54. Protected Chanel Pattern
  55. 55. Protected Chanel Pattern - Range checking  Data in the required range? - Reasonableness checks  data in the expected range for the given situation? - Consistency checks  is the value consistent with other data being computed? - Backwards transformational checks  can we re-compute the input value from the output value? - Data validity checks  parity, checksum, CRC, etc.
  56. 56. Dual Chanel Pattern
  57. 57. Triple Chanel Pattern
  58. 58. Sanity Check Pattern
  59. 59. N-Version Pattern
  60. 60. Further reading: Design Patterns for Embedded Systems in C by Bruce Powel Douglass https://refactoring.guru/design-patterns/ https://embeddedartistry.com/fieldatlas/design-pattern-catalogue/ Object-Oriented Programming With ANSI-C by Axel Schreiner
  61. 61. Thank you!

×