Thread: Tricky request and response structure.

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    34

    Tricky request and response structure.

    Hi Friends,

    I have to implement a request_response function. This function has two arguments one is request command structure and other is response structure which is filled after getting response. Its 8-bytes i.e.,
    Code:
    typdef struct{
    uint16_t data0;
    uint16_t data1;
    uint16_t data2;
    uint16_t data3;
    }data_st;
    
    extern data_st request;
    extern data_st response;
    
    request_response(&request, &response);
    There are 3-4 set of request and response. Which is sent one by one.
    When request0 is sent first response must be discarded as there is 8-bytes(64 bits) gap in response from the other system. When request1 is sent response0 is gotten. Same way request2 is sent response1 is received. Same way request..n ...response..n-1 and request_dummy will be sent in the last to receive response..n.

    I have to collect the correct response for a request. Means need to map response0 for request0 , response1 for request1 and response2 for request2 for further processing.

    How to manage this? Please help.

    Thanks,
    david.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > When request1 is sent response0 is gotten.
    So after you sent the last request, how to do you get it's response, if the response 'n' is only triggered by the arrival of request 'n+1'.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    request_dummy (say 0x00 ) will be sent in the last to receive response'n'.

    Thanks,
    david.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You mean something like this?

    Code:
    type request[5] = { request-1, request-2, request-3, request-4, request-dummy };
    type response[5];
    
    for i = 0 -> 4:
        request_response(&request[i], &response[i]);
    
    for i = 0 -> 3:
        request[i] is associated with response[i + 1]

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Yes..Correct. One time only one request will be sent and response of the previous request will be received. Then next. Please guide how to achieve this?
    Thanks.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by davidmeetsall View Post
    Please guide how to achieve this?
    Is the pseudocode I posted not adequate for expressing the logic you need? If not, explain how it's lacking. Otherwise, what's stopping you from implementing the code using the pseudocode as reference?

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You seem to be suggesting something like this (essentially the same as Matticus suggested).
    Code:
    request_response(&request0, &response_dummy);
    request_response(&request1, &response0);
    request_response(&request2, &response1);
    request_response(&request3, &response2);
    request_response(&request_dummy, &response3);
    Instead of defining all the separate request variables, you could load request with the appropriate values before each call. You could use the same response variable, too, if you transfer the valid data somewhere else as appropriate.

  8. #8
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Actually, Its a state machine implementation for SPI communication in PIC microcontroller. No for loop is used. One request will be sent at the same time previous response will be received. Then Next request etc.. I am confused as I have to process the responses for the same requests in some cases. I hope this makes clear.

  9. #9
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Hi Algorism and matticus,


    I am doing the same as you suggested. But the problem is in a switch case I have to process the result.
    Code:
    case request1:
      send (response1);
    case request2:
      send (response2);
    case request3:
      send (response3);

    How to handle above?


    Thanks.

  10. #10
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Sorry more appropriately...this has to be done next..

    Code:
    case request1.data0:
      send (response1.data2);
    case request2.data0:
      send (response2.data3);
    case request3.data:
      send (response3.data2);

    How to handle above?

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Do you have to process a response as soon as it's received, or can you gather responses then process them after the "3-4 set of request and response" is complete?

  12. #12
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    I have to process a response as soon as it's received. Please suggest the best possible way. Thanks.
    David.

  13. #13
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Do you mean something like this:
    Code:
    typdef struct{
        uint16_t data0;
        uint16_t data1;
        uint16_t data2;
        uint16_t data3;
    } data_st;
    
    enum RequestType { REQUEST_NONE, REQUEST0, REQUEST1, ... };
    
    void handle_request(RequestType request_type) {
    
        static RequestType prev_request = REQUEST_NONE;
    
        static data_st request[4] = {             // however many there are
            { 0x0102, 0x0304, 0x0506, 0x0708 },   // whatever the data is
            // ...
        };
    
        data_st response;
    
    
        switch (request_type) {
    
        case REQUEST0:
            request_response(&request[0], &response);
            if (prev_request != REQUEST_NONE)
                handle_prev_response(prev_request_type, &response);
            prev_request = REQUEST0;
            break;
    
        case REQUEST1:
            request_response(&request[1], &response);
            if (prev_request != REQUEST_NONE)
                handle_prev_response(prev_request, &response);
            prev_request = REQUEST1;
            break;
    
        // etc.
        }
    }
    
    void handle_prev_response(RequestType rt, data_st *response) {
        switch (rt) {
        case REQUEST0:
            // process response for request-0 type
            break;
        case REQUEST1:
            // process response for request-1 type
            break;
        // etc.
        }
    }
    Last edited by algorism; 01-30-2017 at 12:50 PM.

  14. #14
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Hi Algorism,

    Thanks a lot. It looks like this will work and its as per my requirement. I will try and let you know if I face any issue. Thanks.

  15. #15
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Hi Algorism,

    Sorry for informing late. I implemented as per your suggestion making the adjustment in my existing code.
    It works to some extent. The problem I am facing is that, REQUEST0, REQUEST1, REQUEST2, REQUEST3 all work as there is always a next command is there. Like, REQUEST1 is next command for REQUEST0, REQUEST2 is next command for REQUEST1 and so on. But issue here is once REQUEST4 is sent I need to keep repeating sending this command for 3 different modes. Once this is done again REQUEST4 is to be sent for 3 different modes and so on. And data processed for all modes must match with the request as said in my earlier posts.

    Code:
    case REQUEST4:
            request_response(&request[4], &response);
            if (prev_request != REQUEST_NONE)
                handle_prev_response(prev_request, &response);
            prev_request = REQUEST3;
            break;
    //...
    }
    
    
    
    void handle_prev_response(RequestType rt, data_st *response) {
        switch (rt) {
        case REQUEST3:
            // process response for request-3 type
            break;
        case REQUEST4:   ////send same REQUEST4 for 3 different modes and repeat again
            // process response for request-4 type mode1
        break;
    
        case REQUEST4:
            // process response for request-4 type mode2
        break;
        case REQUEST4:
            // process response for request-4 type mode3
    
    
            break;
     case REQUEST4:   ////send same REQUEST4 for 3 different modes and repeat again
            // process response for request-4 type mode1
    
        // etc.
        }
    }
    


    How to achieve this? Please help.

    --david.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request Response for AT Commands
    By sanjyot in forum C Programming
    Replies: 1
    Last Post: 03-08-2014, 10:20 AM
  2. HTTP Request/Response Libraries?
    By jamesjeffery in forum Networking/Device Communication
    Replies: 7
    Last Post: 12-24-2008, 04:44 PM
  3. what is an elegant way to transform request to response?
    By kypronite in forum Networking/Device Communication
    Replies: 1
    Last Post: 12-24-2008, 10:47 AM
  4. C# HTTP request/response
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-02-2008, 06:00 AM
  5. Replies: 3
    Last Post: 10-22-2007, 06:47 AM

Tags for this Thread