#include "contract.h"
-#include <contract-idl.h>
+#include <stdbool.h>
+#include <ul_list.h>
+#include <string.h>
+#include <frsh_error.h>
+
+#define TYPE_VALID(type) \
+ ((type) < _FRSH_CONTRACT_NUM_PARAMS && \
+ (type) >= 0)
+
+struct contract *
+contract_new(void)
+{
+ struct contract *c;
+
+ c = malloc(sizeof(*c));
+ if (c) {
+ memset(c, 0, sizeof(*c));
+ }
+ return c;
+}
+
+void
+contract_destroy(struct contract *contract)
+{
+ /* TODO handle all other deallocations */
+ free(contract);
+}
+
+int
+contract_add_params(struct contract *contract,
+ enum contract_part_type type,
+ void *part_data)
+{
+ if (!TYPE_VALID(type) ||
+ contract->parts[type].state != CONTRACT_PART_EMPTY)
+ return FRSH_ERR_BAD_ARGUMENT;
+
+ contract->parts[type].u.data = part_data;
+ return 0;
+}
+
+int
+contract_del_params(struct contract *contract,
+ enum contract_part_type type)
+{
+ if (TYPE_VALID(type)) {
+ switch (contract->parts[type].state) {
+ case CONTRACT_PART_EMPTY:
+ /* nothing to do */
+ break;
+ case CONTRACT_PART_DATA:
+ free(contract->parts[type].u.data);
+ contract->parts[type].state = CONTRACT_PART_EMPTY;
+ break;
+ case CONTRACT_PART_STREAM:
+ /* TODO: stream_free(contract->parts[type].u.seq); */
+ contract->parts[type].state = CONTRACT_PART_EMPTY;
+ break;
+ }
+ }
+ return 0;
+}
+
+void *
+contract_get_params(struct contract *contract,
+ enum contract_part_type type)
+{
+ if (contract &&
+ TYPE_VALID(type) &&
+ contract->parts[type].state == CONTRACT_PART_DATA) {
+ return contract->parts[type].u.data;
+ } else {
+ return NULL;
+ }
+}
+
+CORBA_boolean
+contract_serialize(CDR_Codec *codec, const struct contract *contract)
+{
+ CORBA_long l;
+ CORBA_long count;
+ CORBA_short s;
+ int i;
+ if (!contract)
+ return false;
+
+ l = contract->resource_id;
+ CORBA_long_serialize(codec, &l);
+ l = contract->resource_type;
+ CORBA_long_serialize(codec, &l);
+ /* TODO: Label */
+
+ count=0;
+ for (i=0; i<_FRSH_CONTRACT_NUM_PARAMS; i++) {
+ if (contract->parts[i].state != CONTRACT_PART_EMPTY)
+ count++;
+ }
+
+ CORBA_long_serialize(codec, &count);
+ for (i=0; i<_FRSH_CONTRACT_NUM_PARAMS; i++) {
+ if (contract->parts[i].state != CONTRACT_PART_EMPTY) {
+ s = i;
+ CORBA_short_serialize(codec, &s);
+
+ }
+ }
+
+}
+
+
+/**********************************/
+/* -----===== FRSH API =====----- */
+/**********************************/
+
+int
+frsh_contract_init(frsh_contract_t *contract)
+{
+ struct frsh_contract *c;
+
+ if (!contract) {
+ return FRSH_ERR_BAD_ARGUMENT;
+ }
+ *contract = c = contract_new();
+ if (!c) {
+ return FOSA_ENOMEM;
+ }
+
+
+ return 0;
+}
+
+int frsh_contract_set_resource_and_label
+ (frsh_contract_t *contract,
+ const frsh_resource_type_t resource_type,
+ const frsh_resource_id_t resource_id,
+ const char *contract_label)
+{
+ struct contract *c = *contract;
+ if (!c)
+ return FRSH_ERR_BAD_ARGUMENT;
+
+ c->resource_type = resource_type;
+ c->resource_id = resource_id;
+ /* contract_label is currently ignored */
+ return 0;
+}
+
+/* int frsh_contract_set_timing_reqs */
+/* (frsh_contract_t *contract, */
+/* const bool d_equals_t, */
+/* const frsh_rel_time_t *deadline, */
+/* const frsh_signal_t budget_overrun_signal, */
+/* const frsh_signal_info_t budget_overrun_siginfo, */
+/* const frsh_signal_t deadline_miss_signal, */
+/* const frsh_signal_info_t deadline_miss_siginfo); */
+/* { */
+/* } */
#ifndef CONTRACT_H
#define CONTRACT_H
+#include <forb/basic_types.h>
+#include <fosa_platform_values.h>
+#include <contract-idl.h>
+
+/*================ From frsh_code_types.h ====================================*/
+
+/** Contract ressource type: processor, network, memory **/
+typedef enum {
+ FRSH_RT_PROCESSOR = 0,
+ FRSH_RT_NETWORK = 1,
+ FRSH_RT_MEMORY = 2,
+ FRSH_RT_DISK = 3
+} frsh_resource_type_t;
+
+/** Ressource Id: processor_id or network_id **/
+/**********************************************/
+typedef fosa_resource_id_t frsh_resource_id_t;
+
+/** Kind of contract: regular, background or dummy **/
+typedef enum {
+ FRSH_CT_REGULAR = 0,
+ FRSH_CT_BACKGROUND = 1,
+ FRSH_CT_DUMMY = 2
+} frsh_contract_type_t;
+
+/*============================================================================*/
+
+typedef struct contract *frsh_contract_t;
+
+enum contract_part_type {
+ FRSH_CONTRACT_BASIC_PARAMS,
+ FRSH_CONTRACT_SPARE_CAPACITY,
+ _FRSH_CONTRACT_NUM_PARAMS
+};
+
+enum contract_part_state {
+ CONTRACT_PART_EMPTY,
+ CONTRACT_PART_DATA,
+ CONTRACT_PART_STREAM
+};
+
+struct contract_part {
+ enum contract_part_state state;
+ union {
+ void *data; /**< Pointer to data according to part_type */
+ stream seq;
+ } u;
+};
+
+struct contract {
+ /** Processor Id or Network Id **/
+ frsh_resource_id_t resource_id;
+
+ /** Whether processor or network **/
+ frsh_resource_type_t resource_type;
+
+ struct contract_part parts[_FRSH_CONTRACT_NUM_PARAMS];
+};
+
+struct contract *
+contract_new(void);
+
+void
+contract_destroy(struct contract *contract);
+
+int
+contract_add_params(struct contract *contract,
+ enum contract_part_type type,
+ void *part_data);
+
+int
+contract_del_params(struct contract *contract,
+ enum contract_part_type type);
+
+void *
+contract_get_params(struct contract *contract,
+ enum contract_part_type type);
+
+CORBA_boolean
+contract_serialize(CDR_Codec *codec, const struct contract *contract);
+
+CORBA_boolean
+contract_deserialize(CDR_Codec *codec, struct contract **contract);
#endif