Menu

Object:Protocol_Protocol

Object (3)
SFENCE

Protocol_Protocol

Protocol_Protocol is standard SFENCE objects with driver.
Object have standard basic driver object functions.

Logic of Protocol_Protocol

Object Protocol_Protocol is used for sending and receiving events.

Special api functions for Protocol_Protocol

Function name Short description
Protocol_Protocol_Start this function may be called before first call of functions Protocol_Protocol_Check, Protocol_Protocol_Can_Send, Protocol_Protocol_Can_Receive, Protocol_Protocol_Send_Message and Protocol_Protocol_Receive_Message
Protocol_Protocol_Stop this function may be called after last call of functions Protocol_Protocol_Check, Protocol_Protocol_Can_Send, Protocol_Protocol_Can_Receive, Protocol_Protocol_Send_Message and Protocol_Protocol_Receive_Message
Protocol_Protocol_Check Can protocol receive or send message?
Protocol_Protocol_Can_Send Can protocol send message at the moment?
Protocol_Protocol_Can_Receive Can protocol receive message at the moment?
Protocol_Protocol_Send_Message send message to protocol
Protocol_Protocol_Receive_Message receive a message from protocol
Protocol_Protocol_Receive_MessageUse receive a message from protocol

Special driver functions for Protocol_Protocol

Function type Function name Short description
fProtocol_Protocol_Start f_start would be call before call other of special API functions of object Protocol_Protocol
fProtocol_Protocol_Stop f_stop would be call after last call of other special API functions.
fProtocol_Protocol_Check f_check check if there is any message for receive or if message can be sent.
fProtocol_Protocol_Can_Send f_can_send check if any message can be sent.
fProtocol_Protocol_Can_Receive f_can_receive check if any message can be received
fProtocol_Protocol_Send_Message f_send_message send message
fProtocol_Protocol_Receive_Message f_receive_message receive message
Example:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "SFENCEProtocol.h"
#include "SFENCEThread.h"
#include "SFENCEEvent.h"
#include "SFENCETime.h"
#include "SFENCEPack.h"
#include "SFENCEError.h"

#define EXAMPLE_fromerror (200200)

...

#define EXAMPLE_fromerror_protocol (EXAMPLE_fromerror+40)

// example of easy thread protocol cycle
enum eSFENCE_Bool Example_ThreadProtocolCycle(struct sProtocol_Protocol *ps_protocol,
                                              struct sThread_Thread *ps_thread,
                                              struct sEvent_Generator *ps_generator,
                                              uintptr_t u0_delay,
                                              struct sPack_Pack *ps_pack)
{
  Mac_Var_Event_Event_Declaration(event)
  Mac_Var_Time_Difference_Declaration(diff)
  enum eSFENCE_Bool b_return;
  uintptr_t u0_diff;

  if (Event_Event_Construct(ps_event, ps_pack)==NULL) 
  {
    Have_Error(ps_pack, EXAMPLE_fromerror_protocol+0);
    return eSFENCE_Bool_Fail;
  }

  if (Time_Difference_Construct(ps_diff, ps_pack)==NULL) 
  {
    Event_Event_Destruct(ps_value, eSFENCE_Bool_false);

    Have_Error(ps_pack, EXAMPLE_fromerror_protocol+1);
    return eSFENCE_Bool_Fail;
  }

  // first protocol start
  b_return = Protocol_Protocol_Start(ps_protocol);
  if (b_return!=eSFENCE_Bool_Success)
  {
    Event_Event_Destruct(ps_value, eSFENCE_Bool_False);
    Time_Difference_Destruct(ps_diff, eSFENCE_Bool_False);

    Have_Error(ps_pack, EXAMPLE_fromerror_protocol+2);
    return b_return;
  }

  // reset time
  Time_Difference_Reset(ps_diff);

  while (1)
  {
    // lock protocol for thread work
    if (Protocol_Protocol_Lock(ps_protocol)!=eSFENCE_Bool_Success)
    {
      Have_Error(ps_pack, EXAMPLE_fromerror_protocol+5);
      break;
    }

    // check protocol
    b_return Protocol_Protocol_Check(ps_protocol);
    if (b_return==eSFENCE_Bool_Fail) 
    {
      Protocol_Protocol_Unlock(ps_protocol);
      Have_Error(ps_pack, EXAMPLE_fromerror_protocol+6);
      break;
    }

    if (b_return==eSFENCE_Bool_True)
    { 
      // can I receive ?
      b_return = Protocol_Protocol_Can_Receive(ps_protocol, NULL);
      if (b_return==eSFENCE_Bool_Fail)
      {
        Protocol_Protocol_Unlock(ps_protocol);
        Have_Error(ps_pack, EXAMPLE_fromerror_protocol+7);
        break;
      }

      if (b_return==eSFENCE_Bool_True)
      {
        // receive message
        b_return = Protocol_Protocol_Receive_Message(ps_protocol, &ps_event, NULL);
        if (b_return==eSFENCE_Bool_Fail)
        {
          Protocol_Protocol_Unlock(ps_protocol);
          Have_Error(ps_pack, EXAMPLE_fromerror_protocol+8);
          break;
        }
        Protocol_Protocol_Unlock(ps_protocol);

        if (b_return==eSFECNE_Bool_True)
        {
          // send message to system
          if (Event_Generator_Lock(ps_generator)!=eSFENCE_Bool_Success)
          {
             Have_Error(ps_pack, EXAMPLE_fromerror_protocol+9);
             break;
          }
          if (Event_Generator_Write_Event(ps_generator, ps_event)!=eSFENCE_Bool_Success)
          {
             Event_Generator_Unlock(ps_generator);

             Have_Error(ps_pack, EXAMPLE_fromerror_protocol+10);
             break;
          }
          Event_Generator_Unlock(ps_generator);

          // check for next message for receive immediately
          continue;
        }
      }
      else Protocol_Protocol_Unlock(ps_protocol);
    }
    else Protocol_Protocol_Unlock(ps_protocol);

    // check if thread going to end
    b_return = Thread_Thread_Have_End(ps_thread);
    if (b_return==eSFENCE_Bool_Fail)
    {
       Have_Error(ps_pack, EXAMPLE_fromerror_protocol+15);
       break;
    }
    if (b_return==eSFENCE_Bool_True)
    {
       Thread_Thread_Ending_Thread(ps_thread);

       // stop protocol
       Protocol_Protocol_Stop(ps_protocol);

       // clean hel objects
       Event_Event_Destruct(ps_event, eSFENCE_Bool_False); 
       Time_Difference_Destruct(ps_diff, eSFENCE_Bool_False); 
       return eSFENCE_Bool_Success;
    }

    // time diff
    u0_diff = Time_Difference_DifferenceWithNewTime(ps_diff);

    // if time difference is lower then wanted delay 
    // between checking for messages for receive
    if (u0_diff<u0_delay)
    {
      // wait some time, in milliseconds 
      b_return = Time_Delay(u0_delay-u0_diff, ps_pack);
      if (b_return==eSFENCE_Bool_Fail)
      {
        Have_Error(ps_pack, EXAMPLE_fromerror_protocol+16);
        break;
      }
    }

    // clear event and repeat cycle
    if (Event_Event_Clear(ps_event)!=eSFENCE_Bool_Success)
    {
      Have_Error(ps_pack, EXAMPLE_fromerror_protocol+17);
      break;
    }
  }
  Event_Event_Destruct(ps_event, eSFENCE_Bool_False); 
  Time_Difference_Destruct(ps_diff, eSFENCE_Bool_False);

  return eSFENCE_Bool_Fail;
}

Related

Wiki: Basic:DriverObject:Functions
Wiki: Basic:DriverObject
Wiki: Object:Protocol_Protocol:DriverFunction:Check
Wiki: Object:Protocol_Protocol:DriverFunction:Start
Wiki: Object:Protocol_Protocol:Function:Can_Receive
Wiki: Object:Protocol_Protocol:Function:Can_Send
Wiki: Object:Protocol_Protocol:Function:Check
Wiki: Object:Protocol_Protocol:Function:Receive_Message
Wiki: Object:Protocol_Protocol:Function:Receive_MessageUse
Wiki: Object:Protocol_Protocol:Function:Send_Message
Wiki: Object:Protocol_Protocol:Function:Start
Wiki: Object:Protocol_Protocol:Function:Stop