/**
 * Copyright (C) 2014 SINTEF <franck.fleurey@sintef.no>
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June 2007;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author sintef
 */
package org.thingml.networkplugins.c;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.sintef.thingml.Configuration;
import org.sintef.thingml.Message;
import org.sintef.thingml.Parameter;
import org.thingml.compilers.Context;
import org.thingml.compilers.c.CCompilerContext;
import org.thingml.compilers.spi.SerializationPlugin;

public class CByteArraySerializerPlugin extends SerializationPlugin {
    CCompilerContext cctx;
    public CByteArraySerializerPlugin() {
        super();
    }

    @Override
    public void setContext(Context ctx) {
        context = ctx;
        cctx = (CCompilerContext) context;
    }

    @Override
    public String generateSerialization(StringBuilder builder, String bufferName, Message m) {
        builder.append("byte " + bufferName + "[" + (cctx.getMessageSerializationSize(m) - 2) + "];\n");

        int HandlerCode = cctx.getHandlerCode(configuration, m);

        builder.append(bufferName + "[0] = (" + HandlerCode + " >> 8) & 0xFF;\n");
        builder.append(bufferName + "[1] =  " + HandlerCode + " & 0xFF;\n\n");

        int j = 2;

        for (Parameter pt : m.getParameters()) {
            builder.append("\n// parameter " + pt.getName() + "\n");
            int i = cctx.getCByteSize(pt.getType(), 0);
            String v = pt.getName();
            if (cctx.isPointer(pt.getType())) {
                // This should not happen and should be checked before.
                throw new Error("ERROR: Attempting to deserialize a pointer (for message " + m.getName() + "). This is not allowed.");
            } else {
                if(!pt.isDefined("ignore", "true")) {
                    builder.append("union u_" + v + "_t {\n");
                    builder.append(cctx.getCType(pt.getType()) + " p;\n");
                    builder.append("byte bytebuffer[" + cctx.getCByteSize(pt.getType(), 0) + "];\n");
                    builder.append("} u_" + v + ";\n");
                    builder.append("u_" + v + ".p = " + v + ";\n");

                    while (i > 0) {
                        i = i - 1;
                        builder.append(bufferName + "[" + j + "] =  (u_" + v + ".bytebuffer[" + i + "] & 0xFF);\n");
                        j++;
                    }
                }
            }
        }
        return j + "";
    }

    @Override
    public void generateParserBody(StringBuilder builder, String bufferName, String bufferSizeName, Set<Message> messages, String sender) {
        builder.append("externalMessageEnqueue((uint8_t *) " + bufferName + ", " + bufferSizeName + ", " + sender + ");\n");
    }

    @Override
    public String getPluginID() {
        return "CByteArraySerializerPlugin";
    }

    @Override
    public List<String> getTargetedLanguages() {
        
        List<String> res = new ArrayList<>();
        res.add("posix");
        res.add("arduino");
        return res;
    }
    
}

@@ -883,122 +883,8 @@
 c;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Set;%0Aimport org.sintef.thingml.Configuration;%0A
 impo
@@ -914,16 +914,16 @@
 essage;%0A
+
 import o
@@ -1093,16 +1093,90 @@
 lugin;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Set;%0A%0A
 public c
@@ -2065,42 +2065,9 @@
 i = 
-cctx.getCByteSize(pt.getType(), 0)
+0
 ;%0A  
@@ -2829,50 +2829,48 @@
  (i 
-%3E
+%3C cctx.getCByteSize(pt.getType(),
  0)
+)
  %7B%0A
-                        i = i - 1;%0A
     
@@ -2978,24 +2978,53 @@
 0xFF);%5Cn%22);%0A
+                        i++;%0A
             

Fix Serialization order